欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP通用代码

程序员文章站 2022-05-10 09:18:32
...
一个是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. $cryptkey = $keya . md5($keya . $keyc);
  15. $key_length = strlen($cryptkey);
  16. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
  17. $string_length = strlen($string);
  18. $result = '';
  19. $box = range(0, 255);
  20. $rndkey = array();
  21. for ($i = 0; $i
  22. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  23. }
  24. for ($j = $i = 0; $i
  25. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  26. $tmp = $box[$i];
  27. $box[$i] = $box[$j];
  28. $box[$j] = $tmp;
  29. }
  30. for ($a = $j = $i = 0; $i
  31. $a = ($a + 1) % 256;
  32. $j = ($j + $box[$a]) % 256;
  33. $tmp = $box[$a];
  34. $box[$a] = $box[$j];
  35. $box[$j] = $tmp;
  36. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  37. }
  38. if ($operation == 'DECODE') {
  39. if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
  40. return substr($result, 26);
  41. } else {
  42. return '';
  43. }
  44. } else {
  45. return $keyc . str_replace('=', '', base64_encode($result));
  46. }
  47. }

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. function addslashes_deep($string)
  13. {
  14. return is_array($string) ? array_map('addslashes_deep', $string) : addslashes($string);
  15. }

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

PHP Code 复制内容到剪贴板

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

PHP Code 复制内容到剪贴板

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

PHP Code 复制内容到剪贴板

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