如何在PHP中找到UTF-8字符串中的字节数?

Luk*_*uke 6 php string byte utf-8 strlen

我从php.net站点有以下函数来确定ASCII和UTF-8字符串中的字节数:

<?php 
/** 
 * Count the number of bytes of a given string. 
 * Input string is expected to be ASCII or UTF-8 encoded. 
 * Warning: the function doesn't return the number of chars 
 * in the string, but the number of bytes. 
 * 
 * @param string $str The string to compute number of bytes 
 * 
 * @return The length in bytes of the given string. 
 */ 
function strBytes($str) 
{ 
  // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT 

  // Number of characters in string 
  $strlen_var = strlen($str); 

  // string bytes counter 
  $d = 0; 

 /* 
  * Iterate over every character in the string, 
  * escaping with a slash or encoding to UTF-8 where necessary 
  */ 
  for ($c = 0; $c < $strlen_var; ++$c) { 

      $ord_var_c = ord($str{$d}); 

      switch (true) { 
          case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)): 
              // characters U-00000000 - U-0000007F (same as ASCII) 
              $d++; 
              break; 

          case (($ord_var_c & 0xE0) == 0xC0): 
              // characters U-00000080 - U-000007FF, mask 110XXXXX 
              // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
              $d+=2; 
              break; 

          case (($ord_var_c & 0xF0) == 0xE0): 
              // characters U-00000800 - U-0000FFFF, mask 1110XXXX 
              // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
              $d+=3; 
              break; 

          case (($ord_var_c & 0xF8) == 0xF0): 
              // characters U-00010000 - U-001FFFFF, mask 11110XXX 
              // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
              $d+=4; 
              break; 

          case (($ord_var_c & 0xFC) == 0xF8): 
              // characters U-00200000 - U-03FFFFFF, mask 111110XX 
              // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
              $d+=5; 
              break; 

          case (($ord_var_c & 0xFE) == 0xFC): 
              // characters U-04000000 - U-7FFFFFFF, mask 1111110X 
              // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
              $d+=6; 
              break; 
          default: 
            $d++;    
      } 
  } 

  return $d; 
} 
?> 
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试与俄罗斯(例如?? ????? ??????? ?????????? ????? ???????? ???? ? ???????. ? ??? ????, ????? ??? ????? ??????? ? ?????? ????? ??? ?????? ???????, ??????? ?????? ??????? ?????? ???? ?????????? ? ???????????? ?????.).它似乎没有返回正确的字节数.

switch语句使用默认条件.任何想法为什么俄罗斯人物不会按预期工作?或者会有更好的选择.

我问这个,因为我需要将UTF-8字符串缩短为一定数量的字节.即我只能发送最大值 在我的情况下,169个字节的JSON数据到iPhone APNS(不包括其他数据包数据).

参考:PHP strlen - Manual(Paolo评论于2007年1月10日03:58)

Mic*_*rdt 5

我问这是因为我需要将utf-8字符串缩短为一定数量的字节。

mb_strcut() 确实可以做到这一点,尽管您可能无法从几乎难以理解的文档中分辨出来。


Phi*_*off 2

如果您希望在使用mbstring.func_overload 2 和 UTF-8 字符串时查找多字节字符串的字节长度,则可以使用以下命令:

mb_strlen($utf8_string, 'latin1');
Run Code Online (Sandbox Code Playgroud)