如何获取 utf-8 字符串中给定字符的代码点编号?
我想获取给定 UTF-8 字符串的 UCS-2 代码点.例如,单词hello"应该变成0068 0065 006C 006C 006F".请注意,字符可以来自任何语言,包括复杂的脚本,如东亚语言.
I want to get the UCS-2 code points for a given UTF-8 string. For example the word "hello" should become something like "0068 0065 006C 006C 006F". Please note that the characters could be from any language including complex scripts like the east asian languages.
因此,问题归结为将给定字符转换为其 UCS-2 代码点"
So, the problem comes down to "convert a given character to its UCS-2 code point"
但是怎么样?拜托,任何形式的帮助都将非常感谢,因为我很着急.
But how? Please, any kind of help will be very very much appreciated since I am in a great hurry.
作为回答发布的提问者回复的转录
感谢您的回复,但需要在 PHP v 4 或 5 而不是 6 中完成.
Thanks for your reply, but it needs to be done in PHP v 4 or 5 but not 6.
该字符串将是来自表单字段的用户输入.
The string will be a user input, from a form field.
我想实现 utf8to16 或 utf8decode 之类的 PHP 版本
I want to implement a PHP version of utf8to16 or utf8decode like
function get_ucs2_codepoint($char)
{
// calculation of ucs2 codepoint value and assign it to $hex_codepoint
return $hex_codepoint;
}
你能帮我用 PHP 还是用上面提到的版本的 PHP 来完成?
Can you help me with PHP or can it be done with PHP with version mentioned above?
推荐答案
Scott Reynen 编写了一个函数来将 UTF-8 转换为 Unicode.我发现它在查看 PHP 文档.
Scott Reynen wrote a function to convert UTF-8 into Unicode. I found it looking at the PHP documentation.
function utf8_to_unicode( $str ) {
$unicode = array();
$values = array();
$lookingFor = 1;
for ($i = 0; $i < strlen( $str ); $i++ ) {
$thisValue = ord( $str[ $i ] );
if ( $thisValue < ord('A') ) {
// exclude 0-9
if ($thisValue >= ord('0') && $thisValue <= ord('9')) {
// number
$unicode[] = chr($thisValue);
}
else {
$unicode[] = '%'.dechex($thisValue);
}
} else {
if ( $thisValue < 128)
$unicode[] = $str[ $i ];
else {
if ( count( $values ) == 0 ) $lookingFor = ( $thisValue < 224 ) ? 2 : 3;
$values[] = $thisValue;
if ( count( $values ) == $lookingFor ) {
$number = ( $lookingFor == 3 ) ?
( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ):
( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 );
$number = dechex($number);
$unicode[] = (strlen($number)==3)?"%u0".$number:"%u".$number;
$values = array();
$lookingFor = 1;
} // if
} // if
}
} // for
return implode("",$unicode);
} // utf8_to_unicode
相关文章