首页 > 技术文章 > 公共方法整合(一)杂项

YFYQ 2020-08-27 10:31 原文

本文主要存储一些自己使用的公共方法。

方法目录:

  1. PHP根据概率产生随机数 
  2. PHP 判断手机号归属地 和 运营商的免费接口
  3. 过滤emoji表情
  4. 生成二维码
  5. 生成随机字符串
  6. 写入log文件 
  7. 生成UUID

 


 

1.PHP根据概率产生随机数

本方法借鉴:壁虎漫步。 大大的博客。

原文链接:https://www.cnblogs.com/phpfensi/p/4242293.html

代码如下:

$data = array(
    'a' => 10 ,
    'b' => 20 ,
    'c' => 30 ,
    'd' => 40
);
echo randomSelect( $data ); 
   
function randomSelect( &$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;
}

  

先忙了,用到的方法会随时更新。

2019年12月11日。

 


 

2.PHP 判断手机号归属地 和 运营商的免费接口

本方法借鉴与:段佳伟的小憩屋  大大的博客。

原文链接:https://www.cnblogs.com/djwhome/p/9483563.html

方法如下:

    $mobile = '15812345600';
    $s = file_get_contents('http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel='.$mobile);
    preg_match_all("/(\w+):'([^']+)/", $s, $m);
    $a = array_combine($m[1], $m[2]);

    $a['province'] = mb_convert_encoding($a['province'], 'utf-8', 'gbk');
    $a['catName'] = mb_convert_encoding($a['catName'], 'utf-8', 'gbk');
    $a['carrier'] = mb_convert_encoding($a['carrier'], 'utf-8', 'gbk');    
    echo "<pre>";
    print_r($a);

因方法返回的中文格式不对,自己转换了下。

2019年12月11日。

 

 


 

3.PHP 过滤表单输入的emoji表情

本方法借鉴与:小king哥 大大的博客。

原文链接:https://www.cnblogs.com/jingmin/p/6706704.html

方法如下:

// 过滤掉emoji表情
function filter_Emoji($str)
{
    $str = preg_replace_callback(    //执行一个正则表达式搜索并且使用一个回调进行替换
            '/./u',
            function (array $match) {
                return strlen($match[0]) >= 4 ? '' : $match[0];
            },
            $str);

     return $str;
 }

2019年 12月25日

方法更新中...

 


 

4.生成二维码

引入的QRtools 类库

地址: https://pan.baidu.com/s/1WU4yj5Owl-qldG9ScgPC9g 提取码: epn1 

方法如下:

#生成二维码
/**
 * 生成二维码
 * @param  [type] $url  对应内容
 * @param  [type] $code 生成名称
 * @return [type]       对应生成地址返回
 */
function reQrCode($url,$code)
{
    import("QRtools", EXTEND_PATH);
    $QRcode = new \QRcode();
    // include_once("QRtools.php");
    // $QRcode = new QRcode();
    //白边 默认4
    $margin=2;
    // 纠错级别:L、M、Q、H
    $level = 'Q';
    // 点的大小:1到10,用于手机端4就可以了
    $size = 4;

    #判定文件夹是否存在
    $dir = $_SERVER['DOCUMENT_ROOT'].'/upload'.'/reqrcode/';
    mkdirs($dir);
    // 下面注释了把二维码图片保存到本地的代码,如果要保存图片,用$fileName替换第二个参数false
    $name = 'upload/reqrcode/'.$code.'.png';
    $fileName = $name;
    // QRcode::png($code, $fileName, $level, $size,$margin);
    $QRcode->png($url, $fileName, $level, $size,$margin);
    $path='\\'.$fileName;
    $path       = strtr($path,'\\','/');
    return $path;
}

#调用方法
$reqrimg = time().rand(10,99);  //二维码名称
$reqrurl = '';  //二维码内容 
reQrCode($reqrurl,$reqrimg);

#判定文件夹是否存在方法
/**
 * 判定文件夹是否存在方法 不存在则生成
 * @param  [type]  $dir  地址
 * @param  integer $mode 权限
 * @return [type]        生成
 */
function mkdirs($dir, $mode = 0777)
{
    if (is_dir($dir) || @mkdir($dir, $mode)) return TRUE;
    if (!mkdirs(dirname($dir), $mode)) return FALSE;
    return @mkdir($dir, $mode);
} 

 

2020年08月27日

 


 

5.生成随机字符串

方法:

/**
 * 生成随机字符串
 * @param  integer $length 生成随机字符串的长度
 * @param  string  $char   组成随机字符串的字符串
 * @return [type]          生成的随机字符串
 */
function str_rand($length = 32, $char = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') 
{
    //0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
    if(!is_int($length) || $length < 0) {
        return false;
    }
    $string = '';
    for($i = $length; $i > 0; $i--) {
        $string .= $char[mt_rand(0, strlen($char) - 1)];
    }
    return $string;
}

2020年08月27日

 

方法持续更新中..


6.生成log 文件

方法:

/**
 * 写入log
 * @param  [type] $file  生成文件名
 * @param  [type] $txt   对应内容
 * @param  string $other [description]编码格式
 * @return [type]        [description]
 */
function ceshilog($file,$txt,$other='wx'){
    $fp =  fopen($file,'ab+');
    fwrite($fp,'-----------'.$other.':'.date('Y-m-d H:i:s').'-----------------'."\r\n");
    if(is_array($txt) || is_object($txt)){
        fwrite($fp,var_export($txt,true));
    }else{
        if($txt==''){
            $txt='// -------------------------------------------------------------';
        }
        fwrite($fp,$txt);
    }
    fwrite($fp,"\r\n\r\n\r\n");
    fclose($fp);
}

 

2020年08月27日

方法持续更新中..

 


 

7.生成UUID

方法如下:

/**
     * PHP生成 UUID
     * @return [type] [description]
     */
    public function  uuid()  
    {  
        $chars = md5(uniqid(mt_rand(), true));  
        $uuid = substr ( $chars, 0, 8 ) . '-'
                . substr ( $chars, 8, 4 ) . '-' 
                . substr ( $chars, 12, 4 ) . '-'
                . substr ( $chars, 16, 4 ) . '-'
                . substr ( $chars, 20, 12 );  
        return $uuid ;  
    }

方法二:

    /**
     * 生成UUID方法
     */
    public function uuidto()
    {
        return uniqid(mt_rand(0,0xffff), true);
    }

 

如果想要了解更多的UUID知识,请传送:https://www.cnblogs.com/Renyi-Fan/p/11870429.html

2020年09月08日

 

 

 

方法持续更新中噻。。。

 

推荐阅读