php Utils辅助类分享_判断是否为手机_按照权重随机排序数据_按照百分比随机排序数据.

 Utils辅助类分享

  判断用户是否为手机访问

  按照权重随机排序数据

  按照百分比随机排序数据.

namespace common\helpers;

class Utils
{

    /**
     * 判断访问用户是否为手机
     */
    static  public function isMobile()
    {
        $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        // echo $user_agent;
        $mobile_agents = Array("ipad","wap","android","iphone","sec","sam","ericsson","240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte","ben","hai","phili");
        $is_mobile = false;
        foreach ($mobile_agents as $device) {
            if (stristr($user_agent, $device)) {
                if ('ipad' == $device) {
                    return $is_mobile;
                }
                $is_mobile = true;
                break;
            }
        }
        return $is_mobile;
    }
    
    /**
     * 按照权重随机排序数据.
     *
     * @param array $weight 权重 例如array('a'=>200,'b'=>300,'c'=>500)
     *
     * @return string key 键名
     */
    static public function weightRoll($weight = array()) {
        $roll = rand(1, array_sum($weight));
        $_tmpW = 0;
        $rollnum = 0;
        foreach ($weight as $k => $v) {
            $min = $_tmpW;
            $_tmpW += $v;
            $max = $_tmpW;
            if ($roll > $min && $roll <= $max) {
                $rollnum = $k;
                break;
            }
        }
        return $rollnum;
    }
    /**
     * 按照百分比随机排序数据.
     * 假设有四个选项:a 占 10%,b占20% ,C占30% , d 占 40% ,原理就是先获取随机数,然后找区间
     * @param array $array 权重 例如array('a'=>0.2,'b'=>0.8)
     *
     * @return string key 键名
     */
    static public function percentRoll( &$array ){
        $datas = $array ;
        if( !is_array($datas) || count($datas) == 0 )
            return ;
        asort($datas); //按照大小排序
        $random = rand(1,100);
        $sum = 0 ;
    
        $flag = '';
        foreach($datas as $key => $data ){
            $sum += $data ;
            if( $random <= $sum ){
                $flag = $key;
                break ;
            }
        }
        if( $flag == '' ){ // 如果传递进来的值的和小于100 ,则取概率最大的。
            $keys = array_keys($datas);
            $flag = $keys[count($keys) - 1] ;
        }
        return $flag;
    }
}

调用方法:
use common\helpers\Utils;
Utils::isMobile();


版权及转载说明

本站原创、转载文章欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创开源社区

本站转载文章版权归原作者所有,如发现本站文章涉嫌侵权请点击「联系我们」反馈,本站将立即给予删除

转载请注明:文章转载自:全分享社区 「http://www.aweb.cc

本文标题:php Utils辅助类分享_判断是否为手机_按照权重随机排序数据_按照百分比随机排序数据.

本文地址:http://www.aweb.cc/Article/detail/id/442.html

Yii rules常用规则 <<上一篇 下一篇>>最新PHP获取客户端浏览以及操作系统信息