常用函数PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | //验证是否为指定长度的字母/数字组合 function fun_text1( $num1 , $num2 , $str ) { return (preg_match( "/^[a-zA-Z0-9]{" . $num1 . "," . $num2 . "}$/" , $str ))?true:false; } //验证是否为指定长度数字 function fun_text2( $num1 , $num2 , $str ) { return (preg_match( "/^[0-9]{" . $num1 . "," . $num2 . "}$/i" , $str ))?true:false; } //验证是否为指定长度汉字 function fun_font( $num1 , $num2 , $str ) { // preg_match("/^[\xa0-\xff]{1,4}$/", $string); return (preg_match( "/^([\x81-\xfe][\x40-\xfe]){" . $num1 . "," . $num2 . "}$/" , $str ))?true:false; } //验证身份证号码 function fun_status( $str ) { return (preg_match( '/(^([\d]{15}|[\d]{18}|[\d]{17}x)$)/' , $str ))?true:false; } //验证邮件地址 function fun_email( $str ){ return (preg_match( '/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$/' , $str ))?true:false; } //验证电话号码 function fun_phone( $str ) { return (preg_match( "/^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/" , $str ))?true:false; } //验证邮编 function fun_zip( $str ) { return (preg_match( "/^[1-9]\d{5}$/" , $str ))?true:false; } //验证url地址 function fun_url( $str ) { return (preg_match( "/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/" , $str ))?true:false; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | //二维数组按照键值排序 function array_sort( $array , $keys , $type = 'asc' ){ //$array为要排序的数组,$keys为要用来排序的键名,$type默认为升序排序 $keysvalue = $new_array = array (); foreach ( $array as $k => $v ){ $keysvalue [ $k ] = $v [ $keys ]; } if ( $type == 'asc' ){ asort( $keysvalue ); } else { arsort( $keysvalue ); } reset( $keysvalue ); foreach ( $keysvalue as $k => $v ){ $new_array [ $k ] = $array [ $k ]; } return $new_array ; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | //中文字符串截取 function msubstr( $str , $start =0, $length , $charset = "utf-8" , $suffix =true){ if (function_exists( "mb_substr" )){ if ( $suffix ) return mb_substr( $str , $start , $length , $charset ). "..." ; else return mb_substr( $str , $start , $length , $charset ); } elseif (function_exists( 'iconv_substr' )) { if ( $suffix ) return iconv_substr( $str , $start , $length , $charset ). "..." ; else return iconv_substr( $str , $start , $length , $charset ); } $re [ 'utf-8' ] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef] [x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/"; $re [ 'gb2312' ] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/" ; $re [ 'gbk' ] = "/[x01-x7f]|[x81-xfe][x40-xfe]/" ; $re [ 'big5' ] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/" ; preg_match_all( $re [ $charset ], $str , $match ); $slice = join( "" , array_slice ( $match [0], $start , $length )); if ( $suffix ) return $slice . "…" ; return $slice ; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | //笛卡尔积生成 function fun( $arr , $tmp = []) { static $list =[]; foreach ( array_shift ( $arr ) AS $v ) { $tmp [] = $v ; if ( $arr ) { fun( $arr , $tmp ); } else { $list [] = $tmp ; } array_pop ( $tmp ); } return $list ; } $arr = [ [1,2,3], [2, 3], [4, 5], [6, 7, 8], ]; var_dump(fun( $arr )); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | //流下载+generator function getLines( $file ) { $f = fopen ( $file , 'r' ); try { while ( $line = fgets ( $f )) { yield $line ; } } finally { fclose( $f ); } } //告诉浏览器这是一个文件流格式的文件 Header ( "Content-type: application/octet-stream" ); //请求范围的度量单位 Header ( "Accept-Ranges: bytes" ); //Content-Length是指定包含于请求或响应中数据的字节长度 Header ( "Accept-Length: " . filesize ( './demo.php' ) ); //用来告诉浏览器,文件是可以当做附件被下载,下载后的文件名称为$file_name该变量的值。 Header ( "Content-Disposition: attachment; filename=demo.php" ); foreach (getLines( "demo.php" ) as $n => $line ) { echo $line ; } exit (); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | ### PHP如何高效获取大文件(1G以上)行数 #### 一次读取一部分数据,计算这部分数据中有多少个换行符,不断循环,效率会比顺序读取内容高。 function countLine( $file ) { $fp = fopen ( $file , 'r' ); $i = 0; while (! feof ( $fp )) { //每次读取2M if ( $data = fread ( $fp , 1024 * 1024 * 2)) { //计算读取到的行数 $num = substr_count( $data , "\n" ); $i += $num ; } } fclose( $fp ); return $i ; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | //驼峰命名转下划线命名 function toUnderScore( $str ) { $dstr = preg_replace_callback( '/([A-Z]+)/' , function ( $matchs ) { return '_' . strtolower ( $matchs [0]); }, $str ); return trim(preg_replace( '/_{2,}/' , '_' , $dstr ), '_' ); } //下划线命名到驼峰命名 function toCamelCase( $str ) { $array = explode ( '_' , $str ); $result = $array [0]; $len = count ( $array ); if ( $len > 1) { for ( $i = 1; $i < $len ; $i ++) { $result .= ucfirst( $array [ $i ]); } } return $result ; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | //无限极分类这个是核心方法 function generateTree( $items ){ $tree = array (); foreach ( $items as $item ){ if (isset( $items [ $item [ 'pid' ]]) || $items [ $item [ 'pid' ]]==0){ $items [ $item [ 'pid' ]][ 'son' ][] =& $items [ $item [ 'id' ]]; } else { $tree [] =& $items [ $item [ 'id' ]]; } } return $tree ; } //这个是从发数据库中取出的数据 $items = array ( 1 => array ( 'id' => 1, 'pid' => 0, 'name' => '安徽省' ), 2 => array ( 'id' => 2, 'pid' => 0, 'name' => '浙江省' ), 3 => array ( 'id' => 3, 'pid' => 1, 'name' => '合肥市' ), 4 => array ( 'id' => 4, 'pid' => 3, 'name' => '长丰县' ), 5 => array ( 'id' => 5, 'pid' => 1, 'name' => '安庆市' ), ); print_r(generateTree( $items )); |
1 2 3 4 5 6 7 8 9 10 11 | //时间相关 echo "今天:" . date ( "Y-m-d" ). "<br>" ; echo "昨天:" . date ( "Y-m-d" , strtotime ( "-1 day" )), "<br>" ; echo "明天:" . date ( "Y-m-d" , strtotime ( "+1 day" )). "<br>" ; echo "一周后:" . date ( "Y-m-d" , strtotime ( "+1 week" )). "<br>" ; echo "一周零两天四小时两秒后:" . date ( "Y-m-d G:H:s" , strtotime ( "+1 week 2 days 4 hours 2 seconds" )). "<br>" ; echo "下个星期四:" . date ( "Y-m-d" , strtotime ( "next Thursday" )). "<br>" ; echo "上个周一:" . date ( "Y-m-d" , strtotime ( "last Monday" )). "<br>" ; echo "一个月前:" . date ( "Y-m-d" , strtotime ( "last month" )). "<br>" ; echo "一个月后:" . date ( "Y-m-d" , strtotime ( "+1 month" )). "<br>" ; echo "十年后:" . date ( "Y-m-d" , strtotime ( "+10 year" )). "<br>" ; |
【推荐】中国电信天翼云云端翼购节,2核2G云服务器一口价38元/年
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步