Heim  >  Artikel  >  Backend-Entwicklung  >  PHP通用代码

PHP通用代码

WBOY
WBOYOriginal
2016-06-23 13:48:01757Durchsuche

一个是curl的post请求函数,主要用于各平台api开发,进行请求接口的处理函数,如果你有多个平台,互相之间要传递数据,用这个函数绝对好用:

PHP Code 复制内容到剪贴板

  1. /** 
  2.  * curl访问程序接口 
  3.  * @param string 
  4.  * @return array 
  5.  */  
  6. function getCurlDate($url, $datas, $key) {  
  7.     $datas['time'] = $_SERVER['REQUEST_TIME'] + 300;  
  8.     $post_data['post'] = urlencode(authcode(serialize($datas), "ENCODE", $key));  
  9. //    echo $url;  
  10.     $ch = curl_init();  
  11.     curl_setopt($ch, CURLOPT_URL, $url);  
  12.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  13.     // 我们在POST数据哦!  
  14.     curl_setopt($ch, CURLOPT_POST, 1);  
  15.     // 把post的变量加上  
  16.     curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);  
  17.     $output = curl_exec($ch);  
  18.  //   dump(curl_error($ch));  
  19.     curl_close($ch);  
  20.     return json_decode($output, true);  
  21. }  
php获取文件的扩展名:

C/C++ Code 复制内容到剪贴板

  1. /** 
  2.  * @获取文件扩展名 
  3.  * @$pic string 图片路径 
  4.  */  
  5. function get_file_ext($pic) {  
  6.     return substr($pic, strrpos($pic, '.') + 1);  
  7. }  
还有一个是可逆的加密、解密函数(加密相同的字符串都会加密成不同的字符串的,非常好用)

PHP Code 复制内容到剪贴板

  1. /** 
  2.  * 字符串加密 
  3.  * @param   $string     需加密的字符 
  4.  * @param   $operation  加密或解密 
  5.  * @param   $key        网站加密key,防止破解 
  6.  * @return  string 
  7.  */  
  8. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {  
  9.     $ckey_length = 4;  
  10.     $key = md5($key ? $key : '^www.itokit.com$');  
  11.     $keya = md5(substr($key, 0, 16));  
  12.     $keyb = md5(substr($key, 16, 16));  
  13.     $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';  
  14.   
  15.     $cryptkey = $keya . md5($keya . $keyc);  
  16.     $key_length = strlen($cryptkey);  
  17.   
  18.     $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;  
  19.     $string_length = strlen($string);  
  20.   
  21.     $result = '';  
  22.     $box = range(0, 255);  
  23.   
  24.     $rndkey = array();  
  25.     for ($i = 0; $i 
  26.         $rndkey[$i] = ord($cryptkey[$i % $key_length]);  
  27.     }  
  28.   
  29.     for ($j = $i = 0; $i 
  30.         $j = ($j + $box[$i] + $rndkey[$i]) % 256;  
  31.         $tmp = $box[$i];  
  32.         $box[$i] = $box[$j];  
  33.         $box[$j] = $tmp;  
  34.     }  
  35.   
  36.     for ($a = $j = $i = 0; $i 
  37.         $a = ($a + 1) % 256;  
  38.         $j = ($j + $box[$a]) % 256;  
  39.         $tmp = $box[$a];  
  40.         $box[$a] = $box[$j];  
  41.         $box[$j] = $tmp;  
  42.         $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));  
  43.     }  
  44.   
  45.     if ($operation == 'DECODE') {  
  46.         if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {  
  47.             return substr($result, 26);  
  48.         } else {  
  49.             return '';  
  50.         }  
  51.     } else {  
  52.         return $keyc . str_replace('=', '', base64_encode($result));  
  53.     }  
  54. }  

PHP Code 复制内容到剪贴板

  1. /** 
  2. * 字符串转十六进制 
  3. * @param unknown_type $s 
  4. */  
  5. function str2hex($s) {  
  6.   $r = "";  
  7.   $hexes = array ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");  
  8.   for ($i=0; $i
  9.   $r .= ($hexes [(ord($s{$i}) >> 4)] . $hexes [(ord($s{$i}) & 0xf)]);  
  10.   return $r;  
  11. }  

PHP Code 复制内容到剪贴板

  1. /** 
  2. * 十六进制转字符串 
  3. * @param unknown_type $s 
  4. */  
  5. function hex2str($s) {  
  6.   $r = "";  
  7.   for ( $i = 0; $i
  8.   {  
  9.     $x1 = ord($s{$i});  
  10.     $x1 = ($x1>=48 && $x1
  11.     $x2 = ord($s{$i+1});  
  12.     $x2 = ($x2>=48 && $x2
  13.     $r .= chr((($x1 
  14.   }  
  15.   return $r;  
  16. }  

PHP Code 复制内容到剪贴板

  1. /** 
  2. * 返回经addslashes处理过的字符串或数组 
  3. * @param $string 需要处理的字符串或数组 
  4. * @return mixed 
  5. */  
  6. function new_addslashes($string){  
  7.   if(!is_array($string)) return addslashes($string);  
  8.   foreach($string as $key => $val) $string[$key] = new_addslashes($val);  
  9.   return $string;  
  10. }  
  11.   
  12. /**/  
  13. function addslashes_deep($string)  
  14. {  
  15.   return is_array($string) ? array_map('addslashes_deep', $string) : addslashes($string);  
  16. }  

PHP Code 复制内容到剪贴板

  1. /** 
  2. * 返回经stripslashes处理过的字符串或数组 
  3. * @param $string 需要处理的字符串或数组 
  4. * @return mixed 
  5. */  
  6. function new_stripslashes($string) {  
  7.   if(!is_array($string)) return stripslashes($string);  
  8.   foreach($string as $key => $val) $string[$key] = new_stripslashes($val);  
  9.   return $string;  
  10. }  
  11. /**/  
  12. function stripslashes_deep($string)  
  13. {  
  14.   return is_array($string) ? array_map('stripslashes_deep', $string) : stripslashes($string);  
  15. }  

PHP Code 复制内容到剪贴板

  1. /** 
  2. * 返回经 htmlspecialchars 处理过的字符串或数组 
  3. * @param $string 需要处理的字符串或数组 
  4. * @return mixed 
  5. */  
  6. function new_html_special_chars($string) {  
  7.   if(!is_array($string)) return htmlspecialchars($string);  
  8.   foreach($string as $key => $val) $string[$key] = new_html_special_chars($val);  
  9.   return $string;  
  10. }  

PHP Code 复制内容到剪贴板

  1. /** 
  2.  * 获取请求ip 
  3.  * 
  4.  * @return ip地址 
  5.  */  
  6. function ip() {  
  7.   if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {  
  8.     $ip = getenv('HTTP_CLIENT_IP');  
  9.   } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {  
  10.     $ip = getenv('HTTP_X_FORWARDED_FOR');  
  11.   } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {  
  12.     $ip = getenv('REMOTE_ADDR');  
  13.   } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {  
  14.     $ip = $_SERVER['REMOTE_ADDR'];  
  15.   }  
  16.   return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';  
  17. }  

PHP Code 复制内容到剪贴板

  1. /** 
  2. * 字符截取 支持UTF8/GBK 
  3. * @param $string 
  4. * @param $length 
  5. * @param $dot 
  6. */  
  7. function str_cut($string, $length, $dot = '...') {  
  8.   $strlen = strlen($string);  
  9.   if($strlen 
  10.   $string = str_replace(array(' ',' ', '&', '"', ''', '“', '”', '?', '', '·', '…'), array('∵',' ', '&', '"', "'", '“', '”', '?', '', '·', '…'), $string);  
  11.   $strcut = '';  
  12.   if(strtolower(CHARSET) == 'utf-8') {  
  13.     $length = intval($length-strlen($dot)-$length/3);  
  14.     $n = $tn = $noc = 0;  
  15.     while($n 
  16.       $t = ord($string[$n]);  
  17.       if($t == 9 || $t == 10 || (32 
  18.         $tn = 1; $n++; $noc++;  
  19.       } elseif(194 
  20.         $tn = 2; $n += 2; $noc += 2;  
  21.       } elseif(224 
  22.         $tn = 3; $n += 3; $noc += 2;  
  23.       } elseif(240 
  24.         $tn = 4; $n += 4; $noc += 2;  
  25.       } elseif(248 
  26.         $tn = 5; $n += 5; $noc += 2;  
  27.       } elseif($t == 252 || $t == 253) {  
  28.         $tn = 6; $n += 6; $noc += 2;  
  29.       } else {  
  30.         $n++;  
  31.       }  
  32.       if($noc >= $length) {  
  33.         break;  
  34.       }  
  35.     }  
  36.     if($noc > $length) {  
  37.       $n -= $tn;  
  38.     }  
  39.     $strcut = substr($string, 0, $n);  
  40.     $strcut = str_replace(array('∵', '&', '"', "'", '“', '”', '?', '', '·', '…'), array(' ', '&', '"', ''', '“', '”', '?', '', '·', '…'), $strcut);  
  41.   } else {  
  42.     $dotlen = strlen($dot);  
  43.     $maxi = $length - $dotlen - 1;  
  44.     $current_str = '';  
  45.     $search_arr = array('&',' ', '"', "'", '“', '”', '?', '', '·', '…','∵'); 
  46.     $replace_arr = array('&',' ', '"', ''', '“', '”', '?', '', '·', '…',' ');  
  47.     $search_flip = array_flip($search_arr);  
  48.     for ($i = 0; $i 
  49.       $current_str = ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];  
  50.       if (in_array($current_str, $search_arr)) {  
  51.         $key = $search_flip[$current_str];  
  52.         $current_str = str_replace($search_arr[$key], $replace_arr[$key], $current_str);  
  53.       }  
  54.       $strcut .= $current_str;  
  55.     }  
  56.   }  
  57.   return $strcut.$dot;  
  58. }  

PHP Code 复制内容到剪贴板

  1. /** 
  2. * 产生随机字符串 
  3. * @param        int                $length    输出长度  
  4. * @param        string         $chars     可选的 ,默认为 0123456789 
  5. * @return     string         字符串 
  6. */  
  7. function random($length, $chars = '0123456789') {  
  8.   $hash = '';  
  9.   $max = strlen($chars) - 1;  
  10.   for($i = 0; $i 
  11.     $hash .= $chars[mt_rand(0, $max)];  
  12.   }  
  13.   return $hash;  
  14. }  

PHP Code 复制内容到剪贴板

  1. /** 
  2. * 将字符串转换为数组 
  3. * @param  string  $data  字符串 
  4. * @return  array  返回数组格式,如果,data为空,则返回空数组 
  5. */  
  6. function string2array($data) {  
  7.   if($data == '') return array();  
  8.   eval("\$array = $data;");  
  9.   return $array;  
  10. }  

PHP Code 复制内容到剪贴板

  1. /** 
  2. * 将数组转换为字符串 
  3. * @param  array  $data    数组 
  4. * @param  bool  $isformdata  如果为0,则不使用new_stripslashes处理,可选参数,默认为1 
  5. * @return  string  返回字符串,如果,data为空,则返回空 
  6. */  
  7. function array2string($data, $isformdata = 1) {  
  8.   if($data == '') return '';  
  9.   if($isformdata) $data = new_stripslashes($data);  
  10.   return addslashes(var_export($data, TRUE));  
  11. }  

PHP Code 复制内容到剪贴板

  1. /** 
  2. * 转换字节数为其他单位 
  3. * @param        string        $filesize        字节大小 
  4. * @return        string        返回大小 
  5. */  
  6. function sizecount($filesize) {  
  7.         if ($filesize >= 1073741824) {  
  8.                 $filesize = round($filesize / 1073741824 * 100) / 100 .' GB';  
  9.         } elseif ($filesize >= 1048576) {  
  10.                 $filesize = round($filesize / 1048576 * 100) / 100 .' MB';  
  11.         } elseif($filesize >= 1024) {  
  12.                 $filesize = round($filesize / 1024 * 100) / 100 . ' KB';  
  13.         } else {  
  14.                 $filesize = $filesize.' Bytes';  
  15.         }  
  16.         return $filesize;  
  17. }  

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:discuz中这样的sql如何写Nächster Artikel:php正则替换问题