作者: xiaogang

  • ffmpeg aac_he 提高音质转码命令

    ffmpeg -i "DjSam-打造月半小夜曲包房气氛国粤语串烧(DJ版).mp3" -threads 2 -c:a libfdk_aac -profile:a aac_he -b:a 32k -y "DjSam-打造月半小夜曲包房气氛国粤语串烧(DJ版).m4a"
    

    文件名有空格需要加””号

  • yii2表单验证方法

    return
    [
    [['username','password','email','phone','images'],'required','message'=>'不能为空'],
    //验证唯一性
    [['username'], 'unique','targetClass' => '\backend\models\Verification','message'=>'用户名不能一致'],
    //验证长度
    ['username', 'string', 'min' => 2, 'max' => 8,"tooLong"=>"太长了", "tooShort"=>"太短了"],
    //正则验证
    ['username','match','pattern'=>'/^[a-z]{1,}$/','message'=>'用户名必须为字母'],
    ['password','match','pattern'=>'/^[0-9]{6,12}$/','message'=>'密码必须是数字,且大于6为小于12位'],
    ['email', 'unique', 'targetClass' => '\backend\models\Verification', 'message' => '邮箱名已存在.'],
    //邮箱验证
    ['email','match','pattern'=>'/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/','message'=>'邮箱格式不正确'],
    //手机号验证
    ['phone','match','pattern'=>'/^1[0-9]{10}$/','message'=>'手机号格式不正确'],
     ];
    
  • YII2默认控制器

    /**
    配置文件/config/web.php 
    写到$config = 
    [
    'defaultRoute'=>'link-list/index',
    ]
    **/
    'defaultRoute'=>'link-list/index',
    
  • YII2 联合查询

    //方法get+models使用方法 LinkClass.name
    public function getLinkClass()
     {
      return $this->hasOne(LinkClass::className(), ['id' => 'flid']);
     }
    
  • PHP 模拟post进行url请求 并输出返回内容

         /**
         * 模拟post进行url请求
         * @param string $url
         * @param string $param
         */
        function request_post($url = '', $param = '') {
            if (empty($url) || empty($param)) {
                return false;
            }
            
            $postUrl = $url;
            $curlPost = $param;
            $ch = curl_init();//初始化curl
            curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
            curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
            curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
            curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
            $data = curl_exec($ch);//运行curl
            curl_close($ch);
            
            return $data;
        }

     

  • 获取URL跳转后的真实URL

    获取文件或跳转后的真实URL地址
    第一种方法

    function getrealurl($url){
    $header = get_headers($url,1);
    if (strpos($header[0],'301') || strpos($header[0],'302')) {
        if(is_array($header['Location'])) {
            return $header['Location'][count($header['Location'])-1];
        }else{
            return $header['Location'];
        }
    }else {
        return $url;
    }
    }
    //使用方法 
    echo getrealurl($url);
    

    第二种方法CURL

    function curl_post_302($url,$data=null) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 获取转向后的内容 
        $data = curl_exec($ch);
        $Headers = curl_getinfo($ch); 
        curl_close($ch);
        if($data != $Headers){ 
            return $Headers["url"]; 
            }else{
                return false;
            }
    }
    //使用方法
    echo curl_post_302($url);
    
  • PHP 截取两个指定字符中间内容

    适合采集等程序使用

    function intercept_str($start,$end,$str)
    {
    if(empty($start)||empty($end)||empty($str))return "参数不正确";
    $strarr=explode($start,$str);
    $str=$strarr[1];
    $strarr=explode($end,$str);
    return $strarr[0];
    }
    //使用方法
    echo intercept_str('www.','.com','www.dj3w.com') //将返回'dj3w