收集了一些便捷的方法
unicode
function unicode_encode($strLong) {
$strArr = preg_split('/(?<!^)(?!$)/u', $strLong);//拆分字符串为数组(含中文字符)
$resUnicode = '';
foreach ($strArr as $str)
{
$bin_str = '';
$arr = is_array($str) ? $str : str_split($str);//获取字符内部数组表示,此时$arr应类似array(228, 189, 160)
foreach ($arr as $value)
{
$bin_str .= decbin(ord($value));//转成数字再转成二进制字符串,$bin_str应类似111001001011110110100000,如果是汉字"你"
}
$bin_str = preg_replace('/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/', '$1$2$3', $bin_str);//正则截取, $bin_str应类似0100111101100000,如果是汉字"你"
$unicode = dechex(bindec($bin_str));//返回unicode十六进制
$_sup = '';
for ($i = 0; $i < 4 - strlen($unicode); $i++)
{
$_sup .= '0';//补位高字节 0
}
$str = '\\u' . $_sup . $unicode; //加上 \u 返回
$resUnicode .= $str;
}
return $resUnicode;
}
function unicode_decode($name)
{
// 转换编码,将Unicode编码转换成可以浏览的utf-8编码
$pattern = '/([\w]+)|(\\\u([\w]{4}))/i';
preg_match_all($pattern, $name, $matches);
if (!empty($matches))
{
$name = '';
for ($j = 0; $j < count($matches[0]); $j++)
{
$str = $matches[0][$j];
if (strpos($str, '\\u') === 0)
{
$code = base_convert(substr($str, 2, 2), 16, 10);
$code2 = base_convert(substr($str, 4), 16, 10);
$c = chr($code).chr($code2);
$c = iconv('UCS-2', 'UTF-8', $c);
$name .= $c;
}
else
{
$name .= $str;
}
}
}
return $name;
}
RSA密钥格式化
/**
* 格式化RSA密钥
* @param $key
* @param string $prefix
* @param string $suffix
* @return string
*/
public function formatRsaKey($key, $prefix = "-----BEGIN PRIVATE KEY-----", $suffix = "-----BEGIN PRIVATE KEY-----")
{
return $prefix . "\n" . wordwrap($key, 64, "\n", true) . "\n" . $suffix;
}
获取字符串中间的字符串
/**
* 获取字符串中间的字符串
* @param string $str 源字符串
* @param string $headStr 开始字符串
* @param string $footerStr 结尾字符串
* @return bool|string
*/
function getBetweenStr($str, $headStr, $footerStr)
{
$start = strpos($str, $headStr);
$footer = strpos(substr($str,$start), $footerStr);
return substr($str, $start, $footer);
}
UUID
function uuid() {
$uuid = '';
$uid = uniqid("uuid", true);
$data = microtime(true);
$hash = hash('md5', $uid . $uuid . md5($data));
$uuid = substr($hash, 0, 8) . substr($hash, 8, 4) . substr($hash, 12, 4) . substr($hash, 16, 4) . substr($hash, 20, 12);
return strtolower($uuid);
}
下划线转驼峰
/**
* 下划线转驼峰
* step1.原字符串转小写,原字符串中的分隔符用空格替换,在字符串开头加上分隔符
* step2.将字符串中每个单词的首字母转换为大写,再去空格,去字符串首部附加的分隔符.
*/
function camelize($uncamelized_words,$separator='_')
{
$uncamelized_words = $separator. str_replace($separator, " ", strtolower($uncamelized_words));
return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator );
}
驼峰转下划线
/**
* 驼峰命名转下划线命名
* 小写和大写紧挨一起的地方,加上分隔符,然后全部转小写
*/
function uncamelize($camelCaps,$separator='_')
{
return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
}
base64编码字符还原成文件
/**
* base64编码字符还原成文件
* @param $file_path 文件路径
* @param $base64_file_str base64编码字符
* @return mixed
*/
function file_base64_decode($file_path, $base64_file_str){
return file_put_contents($file_path,base64_decode(explode(',',$base64_file_str)[1]));
}
文件转base64编码字符
/**
* 文件转base64编码字符
* @param $file_path 文件路径
* @param $mime_type 文件mime_type
* @return string
*/
function file_base64_encode($file_path,$mime_type){
$base64_template = 'data:%s;base64,%s';
$base64_code = base64_encode(file_get_contents($file_path));
return sprintf($base64_template,$mime_type,$base64_code);
}
从url获取表单格式文件数组
/**
* 从url获取表单格式文件数组
* @param $url
* @return mixed
*/
function get_form_format_file_from_url($url){
$tempnam = tempnam(sys_get_temp_dir(),'prefix');
file_put_contents($tempnam,file_get_contents($url));
$file['name'] = pathinfo($url,PATHINFO_BASENAME);
$file_mimetype = 'application/octet-stream';
if (function_exists('finfo_file')){
$file_mimetype = finfo_file(finfo_open(FILEINFO_MIME_TYPE),$tempnam);
}
$file['type'] = $file_mimetype;
$file['ext'] = pathinfo($url,PATHINFO_EXTENSION);
$file['size'] = filesize($tempnam);
$file['temp_name'] = $tempnam;
$file['error'] = 0;
return $file;
}
返回CURLFile对象
/**
* 从公网地址返回CURLFile对象
* @param $url
* @return CURLFile
*/
function getLocaleFileFromUrl($url){
$tempnam = tempnam(sys_get_temp_dir(),'prefix');
file_put_contents($tempnam,file_get_contents($url));//$url可以是公网地址
$file_mimetype = 'application/octet-stream';
if (function_exists('finfo_file')){
$file_mimetype = finfo_file(finfo_open(FILEINFO_MIME_TYPE),$tempnam);
}
$file_basename = pathinfo($url,PATHINFO_BASENAME);//从路径中获取文件(包含后缀)
return new CURLFile($tempnam,$file_mimetype,$file_basename);
}
数组转换成sql
/**
* 数组转换成sql
* @param array $data 数组
* @return string
*/
function format_array_to_sql($data){
$sql_array = [];
foreach ($data as $key => $value) {
$sql_array[] = "`{$key}`='{$value}'";
}
return implode(',',$sql_array);
}
Json对象转成PHP可以识别的Json格式
/**
* Json对象转成php可以识别的Json格式(json_decode可以使用的字符串)
* @param string $js_obj_str Json对象字符串
* @return mixed
*/
function js_obj_str2php_json_str($js_obj_str){
return preg_replace(["/([a-zA-Z_]+[a-zA-Z0-9_]*)\s*:/", "/:\s*'(.*?)'/"], ['"\1":', ': "\1"'], $js_obj_str);
}
stdClass类转array
/**
* stdClass类转array
* @param $obj
* @return mixed
*/
function object_to_array($obj){
$_arr = is_object($obj) ? get_object_vars($obj) : $obj;
foreach ($_arr as $key => $val) {
$val = (is_array($val) || is_object($val)) ? object_to_array($val) : $val; $arr[$key] = $val;
}
return $arr;
}
中文星期几
/**
* 中文星期几
* @param $date
* @return mixed
*/
function cnWeek($date){
$arr = ['天','一','二','三','四','五','六'];
return $arr[date('w',strtotime($date))];
}
获取客户端真实IP地址
//获取客户端真实IP地址
function get_proxy_ip(){
$arr_ip_header = [
'HTTP_CDN_SRC_IP',
'HTTP_PROXY_CLIENT_IP',
'HTTP_WL_PROXY_CLIENT_IP',
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'REMOTE_ADDR',
];
$client_ip = 'unknown';
foreach ($arr_ip_header as $key){
if (!empty($_SERVER[$key]) && strtolower($_SERVER[$key]) != 'unknown'){
$client_ip = $_SERVER[$key];
break;
}
}
return $client_ip;
}
xml转array
/**
* xml转array
* @param $xml
* @return mixed
*/
function xml2array($xml){
return json_decode(json_encode(simplexml_load_string($xml)), TRUE);
}