收集了一些便捷的方法

unicode

  1. function unicode_encode($strLong) {
  2. $strArr = preg_split('/(?<!^)(?!$)/u', $strLong);//拆分字符串为数组(含中文字符)
  3. $resUnicode = '';
  4. foreach ($strArr as $str)
  5. {
  6. $bin_str = '';
  7. $arr = is_array($str) ? $str : str_split($str);//获取字符内部数组表示,此时$arr应类似array(228, 189, 160)
  8. foreach ($arr as $value)
  9. {
  10. $bin_str .= decbin(ord($value));//转成数字再转成二进制字符串,$bin_str应类似111001001011110110100000,如果是汉字"你"
  11. }
  12. $bin_str = preg_replace('/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/', '$1$2$3', $bin_str);//正则截取, $bin_str应类似0100111101100000,如果是汉字"你"
  13. $unicode = dechex(bindec($bin_str));//返回unicode十六进制
  14. $_sup = '';
  15. for ($i = 0; $i < 4 - strlen($unicode); $i++)
  16. {
  17. $_sup .= '0';//补位高字节 0
  18. }
  19. $str = '\\u' . $_sup . $unicode; //加上 \u 返回
  20. $resUnicode .= $str;
  21. }
  22. return $resUnicode;
  23. }
  24. function unicode_decode($name)
  25. {
  26. // 转换编码,将Unicode编码转换成可以浏览的utf-8编码
  27. $pattern = '/([\w]+)|(\\\u([\w]{4}))/i';
  28. preg_match_all($pattern, $name, $matches);
  29. if (!empty($matches))
  30. {
  31. $name = '';
  32. for ($j = 0; $j < count($matches[0]); $j++)
  33. {
  34. $str = $matches[0][$j];
  35. if (strpos($str, '\\u') === 0)
  36. {
  37. $code = base_convert(substr($str, 2, 2), 16, 10);
  38. $code2 = base_convert(substr($str, 4), 16, 10);
  39. $c = chr($code).chr($code2);
  40. $c = iconv('UCS-2', 'UTF-8', $c);
  41. $name .= $c;
  42. }
  43. else
  44. {
  45. $name .= $str;
  46. }
  47. }
  48. }
  49. return $name;
  50. }

RSA密钥格式化

  1. /**
  2. * 格式化RSA密钥
  3. * @param $key
  4. * @param string $prefix
  5. * @param string $suffix
  6. * @return string
  7. */
  8. public function formatRsaKey($key, $prefix = "-----BEGIN PRIVATE KEY-----", $suffix = "-----BEGIN PRIVATE KEY-----")
  9. {
  10. return $prefix . "\n" . wordwrap($key, 64, "\n", true) . "\n" . $suffix;
  11. }

获取字符串中间的字符串

  1. /**
  2. * 获取字符串中间的字符串
  3. * @param string $str 源字符串
  4. * @param string $headStr 开始字符串
  5. * @param string $footerStr 结尾字符串
  6. * @return bool|string
  7. */
  8. function getBetweenStr($str, $headStr, $footerStr)
  9. {
  10. $start = strpos($str, $headStr);
  11. $footer = strpos(substr($str,$start), $footerStr);
  12. return substr($str, $start, $footer);
  13. }

UUID

  1. function uuid() {
  2. $uuid = '';
  3. $uid = uniqid("uuid", true);
  4. $data = microtime(true);
  5. $hash = hash('md5', $uid . $uuid . md5($data));
  6. $uuid = substr($hash, 0, 8) . substr($hash, 8, 4) . substr($hash, 12, 4) . substr($hash, 16, 4) . substr($hash, 20, 12);
  7. return strtolower($uuid);
  8. }

下划线转驼峰

  1. /**
  2.  * 下划线转驼峰
  3.  * step1.原字符串转小写,原字符串中的分隔符用空格替换,在字符串开头加上分隔符
  4.  * step2.将字符串中每个单词的首字母转换为大写,再去空格,去字符串首部附加的分隔符.
  5.  */
  6. function camelize($uncamelized_words,$separator='_')
  7. {
  8. $uncamelized_words = $separator. str_replace($separator, " ", strtolower($uncamelized_words));
  9. return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator );
  10. }

驼峰转下划线

  1. /**
  2.  * 驼峰命名转下划线命名
  3.  * 小写和大写紧挨一起的地方,加上分隔符,然后全部转小写
  4.  */
  5. function uncamelize($camelCaps,$separator='_')
  6. {
  7. return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
  8. }

base64编码字符还原成文件

  1. /**
  2. * base64编码字符还原成文件
  3. * @param $file_path 文件路径
  4. * @param $base64_file_str base64编码字符
  5. * @return mixed
  6. */
  7. function file_base64_decode($file_path, $base64_file_str){
  8. return file_put_contents($file_path,base64_decode(explode(',',$base64_file_str)[1]));
  9. }

文件转base64编码字符

  1. /**
  2. * 文件转base64编码字符
  3. * @param $file_path 文件路径
  4. * @param $mime_type 文件mime_type
  5. * @return string
  6. */
  7. function file_base64_encode($file_path,$mime_type){
  8. $base64_template = 'data:%s;base64,%s';
  9. $base64_code = base64_encode(file_get_contents($file_path));
  10. return sprintf($base64_template,$mime_type,$base64_code);
  11. }

从url获取表单格式文件数组

  1. /**
  2. * 从url获取表单格式文件数组
  3. * @param $url
  4. * @return mixed
  5. */
  6. function get_form_format_file_from_url($url){
  7. $tempnam = tempnam(sys_get_temp_dir(),'prefix');
  8. file_put_contents($tempnam,file_get_contents($url));
  9. $file['name'] = pathinfo($url,PATHINFO_BASENAME);
  10. $file_mimetype = 'application/octet-stream';
  11. if (function_exists('finfo_file')){
  12. $file_mimetype = finfo_file(finfo_open(FILEINFO_MIME_TYPE),$tempnam);
  13. }
  14. $file['type'] = $file_mimetype;
  15. $file['ext'] = pathinfo($url,PATHINFO_EXTENSION);
  16. $file['size'] = filesize($tempnam);
  17. $file['temp_name'] = $tempnam;
  18. $file['error'] = 0;
  19. return $file;
  20. }

返回CURLFile对象

  1. /**
  2. * 从公网地址返回CURLFile对象
  3. * @param $url
  4. * @return CURLFile
  5. */
  6. function getLocaleFileFromUrl($url){
  7. $tempnam = tempnam(sys_get_temp_dir(),'prefix');
  8. file_put_contents($tempnam,file_get_contents($url));//$url可以是公网地址
  9. $file_mimetype = 'application/octet-stream';
  10. if (function_exists('finfo_file')){
  11. $file_mimetype = finfo_file(finfo_open(FILEINFO_MIME_TYPE),$tempnam);
  12. }
  13. $file_basename = pathinfo($url,PATHINFO_BASENAME);//从路径中获取文件(包含后缀)
  14. return new CURLFile($tempnam,$file_mimetype,$file_basename);
  15. }

数组转换成sql

  1. /**
  2. * 数组转换成sql
  3. * @param array $data 数组
  4. * @return string
  5. */
  6. function format_array_to_sql($data){
  7. $sql_array = [];
  8. foreach ($data as $key => $value) {
  9. $sql_array[] = "`{$key}`='{$value}'";
  10. }
  11. return implode(',',$sql_array);
  12. }

Json对象转成PHP可以识别的Json格式

  1. /**
  2. * Json对象转成php可以识别的Json格式(json_decode可以使用的字符串)
  3. * @param string $js_obj_str Json对象字符串
  4. * @return mixed
  5. */
  6. function js_obj_str2php_json_str($js_obj_str){
  7. return preg_replace(["/([a-zA-Z_]+[a-zA-Z0-9_]*)\s*:/", "/:\s*'(.*?)'/"], ['"\1":', ': "\1"'], $js_obj_str);
  8. }

stdClass类转array

  1. /**
  2. * stdClass类转array
  3. * @param $obj
  4. * @return mixed
  5. */
  6. function object_to_array($obj){
  7. $_arr = is_object($obj) ? get_object_vars($obj) : $obj;
  8. foreach ($_arr as $key => $val) {
  9. $val = (is_array($val) || is_object($val)) ? object_to_array($val) : $val; $arr[$key] = $val;
  10. }
  11. return $arr;
  12. }

中文星期几

  1. /**
  2. * 中文星期几
  3. * @param $date
  4. * @return mixed
  5. */
  6. function cnWeek($date){
  7. $arr = ['天','一','二','三','四','五','六'];
  8. return $arr[date('w',strtotime($date))];
  9. }

获取客户端真实IP地址

  1. //获取客户端真实IP地址
  2. function get_proxy_ip(){
  3. $arr_ip_header = [
  4. 'HTTP_CDN_SRC_IP',
  5. 'HTTP_PROXY_CLIENT_IP',
  6. 'HTTP_WL_PROXY_CLIENT_IP',
  7. 'HTTP_CLIENT_IP',
  8. 'HTTP_X_FORWARDED_FOR',
  9. 'REMOTE_ADDR',
  10. ];
  11. $client_ip = 'unknown';
  12. foreach ($arr_ip_header as $key){
  13. if (!empty($_SERVER[$key]) && strtolower($_SERVER[$key]) != 'unknown'){
  14. $client_ip = $_SERVER[$key];
  15. break;
  16. }
  17. }
  18. return $client_ip;
  19. }

xml转array

  1. /**
  2. * xml转array
  3. * @param $xml
  4. * @return mixed
  5. */
  6. function xml2array($xml){
  7. return json_decode(json_encode(simplexml_load_string($xml)), TRUE);
  8. }