php unicode与字符串互转

阅读原文
php字符串转Unicode编码, Unicode编码转php字符

百度了很多,都一样, 要么不对, 要不就是只是把字符串的汉字转Unicode

经过多次试验查找, 找到了如下方法,

注意:字符串编码必须是utf-8,如果不是自行用icon转一下
//字符串转Unicode编码
function unicode_encode($strLong) {
s t r A r r = p r e g s p l i t ( ′ / ( ? < ! ) ( ? ! strArr = preg_split('/(?strArr=pregsplit(/(?<!)(?!)/u’, $strLong);//拆分字符串为数组(含中文字符)
r e s U n i c o d e = ′ ′ ; f o r e a c h ( resUnicode = ''; foreach ( resUnicode=;foreach(strArr as $str)
{
$bin_str = ‘’;
a r r = i s a r r a y ( arr = is_array( arr=isarray(str) ? s t r : s t r s p l i t ( str : str_split( str:strsplit(str);//获取字符内部数组表示,此时 a r r 应 类 似 a r r a y ( 228 , 189 , 160 ) f o r e a c h ( arr应类似array(228, 189, 160) foreach ( arrarray(228,189,160)foreach(arr as $value)
{
b i n s t r . = d e c b i n ( o r d ( bin_str .= decbin(ord( binstr.=decbin(ord(value));//转成数字再转成二进制字符串,$bin_str应类似111001001011110110100000,如果是汉字"你"
}
b i n s t r = p r e g r e p l a c e ( ′ / . 4 ( . 4 ) . 2 ( . 6 ) . 2 ( . 6 ) bin_str = preg_replace('/^.{4}(.{4}).{2}(.{6}).{2}(.{6}) binstr=pregreplace(/.4(.4).2(.6).2(.6)/’, ‘$1$2$3’, $bin_str);//正则截取, $bin_str应类似0100111101100000,如果是汉字"你"
u n i c o d e = d e c h e x ( b i n d e c ( unicode = dechex(bindec( unicode=dechex(bindec(bin_str));//返回unicode十六进制
s u p = ′ ′ ; f o r ( _sup = ''; for ( sup=;for(i = 0; i < 4 − s t r l e n ( i < 4 - strlen( i<4strlen(unicode); $i++)
{
$_sup .= ‘0’;//补位高字节 0
}
$str = ‘\u’ . $_sup . $unicode; //加上 \u 返回
$resUnicode .= $str;
}
return KaTeX parse error: Expected 'EOF', got '}' at position 13: resUnicode; }̲ //Unicode编码转字符…name)
{
// 转换编码,将Unicode编码转换成可以浏览的utf-8编码
KaTeX parse error: Undefined control sequence: \w at position 15: pattern = '/([\̲w̲]+)|(\\\u([\w]{…pattern, $name, m a t c h e s ) ; i f ( ! e m p t y ( matches); if (!empty( matches);if(!empty(matches))
{
n a m e = ′ ′ ; f o r ( name = ''; for ( name=;for(j = 0; j < c o u n t ( j < count( j<count(matches[0]); $j++)
{
$str = m a t c h e s [ 0 ] [ matches[0][ matches[0][j];
if (strpos($str, ‘\u’) === 0)
{
c o d e = b a s e c o n v e r t ( s u b s t r ( code = base_convert(substr( code=baseconvert(substr(str, 2, 2), 16, 10);
c o d e 2 = b a s e c o n v e r t ( s u b s t r ( code2 = base_convert(substr( code2=baseconvert(substr(str, 4), 16, 10);
c = c h r ( c = chr( c=chr(code).chr($code2);
$c = iconv(‘UCS-2’, ‘UTF-8’, $c);
$name .= $c;
}
else
{
$name .= $str;
}
}
}
return KaTeX parse error: Expected 'EOF', got '}' at position 7: name; }̲ //Unicode编码转字符…str){
$json = ‘{“str”:"’ . $str . ‘"}’;
a r r = j s o n d e c o d e ( arr = json_decode( arr=jsondecode(json, true);
if (empty($arr)) return ‘’;
return $arr[‘str’];
}

echo unicode_encode(‘若水小站:qq963087326’),’
’;
//结果\u82e5\u6c34\u5c0f\u7ad9\u003a\u0071\u0071\u0039\u0036\u0033\u0030\u0038\u0037\u0033\u0032\u0036

echo unicode_decode(’\u82e5\u6c34\u5c0f\u7ad9\u003a\u0071\u0071\u0039\u0036\u0033\u0030\u0038\u0037\u0033\u0032\u0036’);
//结果若水小站:qq9630873

参考地址:https://www.cnblogs.com/cmnull/p/10030899.html

https://www.jb51.net/article/104925.htm(脚本之家网站,里面的unicode_encode字符串转Unicode,经本人测试"小"字不对)

你可能感兴趣的:(笔记)