微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 如何获取utf-8字符串中给定字符的代码点号?

我想得到一个给定的UTF-8字符串的UCS-2代码点.例如,“hello”这个词应该是“0068 0065 006C 006C 006F”.请注意,字符可能来自任何语言,包括东亚语言等复杂脚本.

所以,问题归结为“将给定的字符转换为UCS-2代码点”

但是怎么样请,非常感谢,因为我很匆忙.

提前致谢

提问者答复的转录作为答案

感谢您的回复,但它需要在PHP v 4或5中完成但不是6.

字符串将是用户输入,从表单字段.

我想实现一个PHP版本的utf8to16或utf8decode like

function get_ucs2_codepoint($char)
{
    // calculation of ucs2 codepoint value and assign it to $hex_codepoint
    return $hex_codepoint;
}

你可以用PHP来帮我吗,还是用PHP提供的版本呢?

再次感谢你.

Scott Reynen写了一个功能convert UTF-8 into Unicode.我发现它在 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

原文地址:https://www.jb51.cc/php/140080.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐