一些常用的PHP函数(整理)

  1.   
  2. /** 
  3.  * 获取当前毫秒 
  4.  * @return string 
  5.  */  
  6. function get_millisecond() {  
  7.     $t = explode ( " ", microtime () );  
  8.     $t = $t [1] . ($t [0] * 1000);  
  9.     $t2 = explode ( "."$t );  
  10.     return $t2 [0];  
  11. }  
  12. /** 
  13.  * curl模拟http/https post请求 
  14.  * @param string $url           请求网址 
  15.  * @param array $data           请求参数 
  16.  * @return string               网址内容 
  17.  */  
  18. function curl_post($url,$data=array()){  
  19.     $ch = curl_init($url);  
  20.     curl_setopt($ch, CURLOPT_RETURNTRANSFER,true) ; // 获取数据返回  
  21.     curl_setopt($ch, CURLOPT_POST,true) ; // 启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。  
  22.     curl_setopt($ch, CURLOPT_POSTFIELDS,$data); // 在HTTP中的“POST”操作。如果要传送一个文件,需要一个@开头的文件名  
  23.     if(substr($url,0,5)=='https'){  
  24.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
  25.     }  
  26.     $content=curl_exec($ch);  
  27.     curl_close($ch) ;  
  28.     return $content;  
  29. }  
  30. /** 
  31.  * curl模拟http get请求 
  32.  * @param string $url           请求网址 
  33.  * @param string|array $data    请求参数 
  34.  * @return string               网址内容 
  35.  */  
  36. function curl_get($url,$data=array()){  
  37.     $url=rtrim($url,'/');  
  38.     if(!empty($data)){  
  39.         if(is_array($data)){  
  40.             $first=true;  
  41.             foreach($data as $k=>$v){  
  42.                 if($first){  
  43.                     $url.='?';  
  44.                     $first=false;  
  45.                 }else{  
  46.                     $url.='&';  
  47.                 }  
  48.                 $url.="{$k}={$v}";  
  49.             }  
  50.         }else{  
  51.             $data=ltrim('?',$data);  
  52.             $url.='?'.$data;  
  53.         }  
  54.     }  
  55.     $ch = curl_init($url);  
  56.     curl_setopt($ch,CURLOPT_HEADER,false);  
  57.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回  
  58.     if(substr($url,0,5)=='https'){  
  59.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
  60.     }  
  61.     $content = curl_exec($ch);  
  62.     curl_close($ch);  
  63.     return $content;  
  64. }  
  65. /** 
  66.  * 二维数组按键值排序 
  67.  * @param array $arr        二维数组 
  68.  * @param string $keys      键值 
  69.  * @param string $type      升序:asc,降序:desc(else) 
  70.  * @return array 
  71.  */  
  72. function array_sort($arr,$keys,$type='asc'){  
  73.     $keysvalue = $new_array = array();  
  74.     foreach ($arr as $k=>$v){  
  75.         $keysvalue[$k] = $v[$keys];  
  76.     }  
  77.     if($type == 'asc'){  
  78.         asort($keysvalue);  
  79.     }else{  
  80.         arsort($keysvalue);  
  81.     }  
  82.     reset($keysvalue);  
  83.     foreach ($keysvalue as $k=>$v){  
  84.         $new_array[$k] = $arr[$k];  
  85.     }  
  86.     return $new_array;  
  87. }  
  88. /** 
  89.  * 获取客户端ip 
  90.  * @param number $type 
  91.  * @return string 
  92.  */  
  93. function get_client_ip($type = 0) {  
  94.     $type       =  $type ? 1 : 0;  
  95.     static $ip  =   NULL;  
  96.     if ($ip !== NULL) return $ip[$type];  
  97.     if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {  
  98.         $arr    =   explode(','$_SERVER['HTTP_X_FORWARDED_FOR']);  
  99.         $pos    =   array_search('unknown',$arr);  
  100.         if(false !== $pos) unset($arr[$pos]);  
  101.         $ip     =   trim($arr[0]);  
  102.     }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {  
  103.         $ip     =   $_SERVER['HTTP_CLIENT_IP'];  
  104.     }elseif (isset($_SERVER['REMOTE_ADDR'])) {  
  105.         $ip     =   $_SERVER['REMOTE_ADDR'];  
  106.     }  
  107.     // IP地址合法验证  
  108.     $long = sprintf("%u",ip2long($ip));  
  109.     $ip   = $long ? array($ip$long) : array('0.0.0.0', 0);  
  110.     return $ip[$type];  
  111. }  
  112. /** 
  113.  * 将字符串转换为驼峰式命名 
  114.  *  
  115.  * @param string $str            
  116.  * @param boolean $big 
  117.  *          true大驼峰式 false小驼峰式 
  118.  * @return string 
  119.  */  
  120. function hump_type($str$big = false) {  
  121.     $str = strtolower ( $str );  
  122.     $big and ucfirst ( $str );  
  123.     $str = preg_replace ( "/_([a-zA-Z])/e""strtoupper('\\1')"$str );  
  124.     return $str;  
  125. }  
  126.   
  127. /** 
  128.  * 遍历删除文件夹 
  129.  * 
  130.  * @param string $path 
  131.  *          文件夹地址 
  132.  * @return boolean 
  133.  */  
  134. function delete_files($path) {  
  135.     if (is_file ( $path )) {  
  136.         return unlink ( $path );  
  137.     }  
  138.     if (is_dir ( $path )) {  
  139.         $handle = opendir ( $path );  
  140.         if ($handle != false) {  
  141.             while ( false !== ($file = readdir ( $handle )) ) {  
  142.                 if (in_array ( $filearray (  
  143.                         '.',  
  144.                         '..'   
  145.                 ) ))  
  146.                     continue;  
  147.                 $file = $path . '/' . $file;  
  148.                 if (is_dir ( $file )) {  
  149.                     delete_files ( $file );  
  150.                 } else if (is_file ( $file )) {  
  151.                     if (unlink ( $file ) == false)  
  152.                         return false;  
  153.                 }  
  154.             }  
  155.             closedir ( $handle );  
  156.         }  
  157.         return true;  
  158.     }  
  159. }  
  160. /** 
  161.  * 截取字符串(可截取utf8的) 
  162.  * 
  163.  * @param string $str            
  164.  * @param int $start             
  165.  * @param int $length            
  166.  * @param string $trim           
  167.  * @param string $charset            
  168.  * @return string 
  169.  */  
  170. function sub($str$start$length$trim = "..."$charset = 'UTF-8') {  
  171.     $length+=2;  
  172.     if (function_exists ( 'mb_get_info' )) {  
  173.         $iLength = mb_strlen ( $str$charset );  
  174.         $str = mb_substr ( $str$start$length$charset );  
  175.         if($length$iLength - $start){  
  176.             $length-=2;  
  177.             return mb_substr ( $str$start$length$charset ).$trim;  
  178.         }else{  
  179.             return $str;  
  180.         }  
  181.     } else {  
  182.         preg_match_all ( "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"$str$info );  
  183.         $str = join ( ""array_slice ( $info [0], $start$length ) );  
  184.         return ($length < (sizeof ( $info [0] ) - $start)) ? $str . $trim : $str;  
  185.     }  
  186. }  
  187. /** 
  188.  * 游览器友好输出 
  189.  * 无线参数 
  190.  */  
  191. function dump() {  
  192.     $data=func_get_args();  
  193.     ob_start ();  
  194.     foreach($data as $v){  
  195.         var_dump ( $v );  
  196.     }  
  197.     $output = ob_get_clean ();  
  198.     if (! extension_loaded ( 'xdebug' )) {  
  199.         $output = preg_replace ( '/\]\=\>\n(\s+)/m''] => '$output );  
  200.         $output = '<pre>' . htmlspecialchars ( $output, ENT_QUOTES ) . '</pre>';  
  201.     }  
  202.     echo ($output);  
  203. }  
  204. /** 
  205.  * 从左边查询子串,找到则删除子串,只删除一次 
  206.  * 
  207.  * @param string $str            
  208.  * @param string $find           
  209.  * @return string 
  210.  */  
  211. function substr_left_once($str$find) {  
  212.     $start = strpos ( $str$find );  
  213.     if (is_bool ( $start )) {  
  214.         return $str;  
  215.     } else {  
  216.         return substr ( $str$start + strlen ( $find ), strlen ( $str ) );  
  217.     }  
  218. }  
  219. /** 
  220.  * 从右边查询子串,找到则删除子串,只删除一次 
  221.  * 
  222.  * @param string $str            
  223.  * @param string $find           
  224.  * @return string 
  225.  */  
  226. function substr_right_once($str$find) {  
  227.     $end = strrpos ( $str$find );  
  228.     if (is_bool ( $end )) {  
  229.         return $str;  
  230.     } else {  
  231.         return substr ( $str, 0, $end );  
  232.     }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值