• <bdo id='qCHHi'></bdo><ul id='qCHHi'></ul>

    <small id='qCHHi'></small><noframes id='qCHHi'>

    <legend id='qCHHi'><style id='qCHHi'><dir id='qCHHi'><q id='qCHHi'></q></dir></style></legend>
    <tfoot id='qCHHi'></tfoot>

    1. <i id='qCHHi'><tr id='qCHHi'><dt id='qCHHi'><q id='qCHHi'><span id='qCHHi'><b id='qCHHi'><form id='qCHHi'><ins id='qCHHi'></ins><ul id='qCHHi'></ul><sub id='qCHHi'></sub></form><legend id='qCHHi'></legend><bdo id='qCHHi'><pre id='qCHHi'><center id='qCHHi'></center></pre></bdo></b><th id='qCHHi'></th></span></q></dt></tr></i><div id='qCHHi'><tfoot id='qCHHi'></tfoot><dl id='qCHHi'><fieldset id='qCHHi'></fieldset></dl></div>

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

        时间:2023-09-22
        • <bdo id='f7HwM'></bdo><ul id='f7HwM'></ul>
        • <legend id='f7HwM'><style id='f7HwM'><dir id='f7HwM'><q id='f7HwM'></q></dir></style></legend>

              <small id='f7HwM'></small><noframes id='f7HwM'>

                <i id='f7HwM'><tr id='f7HwM'><dt id='f7HwM'><q id='f7HwM'><span id='f7HwM'><b id='f7HwM'><form id='f7HwM'><ins id='f7HwM'></ins><ul id='f7HwM'></ul><sub id='f7HwM'></sub></form><legend id='f7HwM'></legend><bdo id='f7HwM'><pre id='f7HwM'><center id='f7HwM'></center></pre></bdo></b><th id='f7HwM'></th></span></q></dt></tr></i><div id='f7HwM'><tfoot id='f7HwM'></tfoot><dl id='f7HwM'><fieldset id='f7HwM'></fieldset></dl></div>
                  <tbody id='f7HwM'></tbody>

                1. <tfoot id='f7HwM'></tfoot>

                  本文介绍了如何获取 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
                  

                  这篇关于如何获取 utf-8 字符串中给定字符的代码点编号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将 PHP 默认编码设置为 utf-8? 下一篇:有什么方法可以用编码 UTF-8 而不是 Unicode 返回

                  相关文章

                  最新文章

                  <legend id='F87YB'><style id='F87YB'><dir id='F87YB'><q id='F87YB'></q></dir></style></legend>
                  <tfoot id='F87YB'></tfoot>
                      <bdo id='F87YB'></bdo><ul id='F87YB'></ul>
                  1. <i id='F87YB'><tr id='F87YB'><dt id='F87YB'><q id='F87YB'><span id='F87YB'><b id='F87YB'><form id='F87YB'><ins id='F87YB'></ins><ul id='F87YB'></ul><sub id='F87YB'></sub></form><legend id='F87YB'></legend><bdo id='F87YB'><pre id='F87YB'><center id='F87YB'></center></pre></bdo></b><th id='F87YB'></th></span></q></dt></tr></i><div id='F87YB'><tfoot id='F87YB'></tfoot><dl id='F87YB'><fieldset id='F87YB'></fieldset></dl></div>

                    <small id='F87YB'></small><noframes id='F87YB'>