是否可以输入字符并取回unicode值?例如,我可以在HTML中输入&#12103来输出“⽇”,是否可以将该字符作为函数的参数并获得数字作为输出而无需构建unicode表?
$val = someFunction("⽇");//returns 12103
还是相反?
$val2 = someOtherFunction(12103);//returns "⽇"
我希望能够将实际字符输出到页面上而不是代码中,并且如果可能的话,我也希望能够从字符中获取代码。我最想要的是php.net/manual/en/function.mb-decode-numericentity.php,但是我无法正常工作,这是我需要的代码还是我走错了轨道?
function _uniord($c) {
if (ord($c{0}) >=0 && ord($c{0}) <= 127)
return ord($c{0});
if (ord($c{0}) >= 192 && ord($c{0}) <= 223)
return (ord($c{0})-192)*64 + (ord($c{1})-128);
if (ord($c{0}) >= 224 && ord($c{0}) <= 239)
return (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
if (ord($c{0}) >= 240 && ord($c{0}) <= 247)
return (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
if (ord($c{0}) >= 248 && ord($c{0}) <= 251)
return (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128);
if (ord($c{0}) >= 252 && ord($c{0}) <= 253)
return (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128);
if (ord($c{0}) >= 254 && ord($c{0}) <= 255) // error
return FALSE;
return 0;
} // function _uniord()
和
function _unichr($o) {
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding('&#'.intval($o).';', 'UTF-8', 'HTML-ENTITIES');
} else {
return chr(intval($o));
}
} // function _unichr()
这是基于以下内容的unichr / uniord的更紧凑的实现pack
:
// code point to UTF-8 string
function unichr($i) {
return iconv('UCS-4LE', 'UTF-8', pack('V', $i));
}
// UTF-8 string to code point
function uniord($s) {
return unpack('V', iconv('UTF-8', 'UCS-4LE', $s))[1];
}
如果您使用的是PHP7.2(或更高版本),则无需定义新函数。Multibyte String扩展有两个功能供您使用!
要获取字符的代码点(即Unicode值),请使用mb_ord();并要从该值中获取特定字符,请使用mb_chr()。
例如:
mb_chr(12103, "utf8"); // ⽇
mb_ord("⽇", "utf8"); // 12103
这也可以(对于了解位移的人,可能比Mark Bakers的回答更具可读性):
public function ordinal($str){
$charString = mb_substr($str, 0, 1, 'utf-8');
$size = strlen($charString);
$ordinal = ord($charString[0]) & (0xFF >> $size);
//Merge other characters into the value
for($i = 1; $i < $size; $i++){
$ordinal = $ordinal << 6 | (ord($charString[$i]) & 127);
}
return $ordinal;
}
您可以使用以下功能
用于编码
string utf8_encode ( string $data )
http://php.net/manual/zh/function.utf8-encode.php
用于解码
string utf8_decode ( string $data )
http://php.net/manual/zh/function.utf8-decode.php
还要检查
http://php.net/manual/zh/function.htmlspecialchars.php
<?php
echo htmlspecialchars_decode("⽇");//will print ⽇
?>
本文地址:http://php.askforanswer.com/wokeyishiyongphphuoquzifudeunicodezhifanzhiyiranma.html
文章标签:php , unicode , utf-8
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
文章标签:php , unicode , utf-8
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
评论已关闭!