php - 如何使用 PHP 查找 UTF-8 字符串中的字节数?

标签 php string utf-8 byte 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; 
} 
?> 

However when I try this with Russian (e.g. По своей природе компьютеры могут работать лишь с числами. И для того, чтобы они могли хранить в памяти буквы или другие символы, каждому такому символу должно быть поставлено в соответствие число. )。它似乎没有返回正确的字节数。

switch 语句使用默认条件。知道为什么俄语字符无法按预期工作吗?或者会有更好的选择。

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

引用:PHP strlen - Manual (Paolo Comment on 10-Jan-2007 03:58)

最佳答案

I am asking this as I need to shorten a utf-8 string to a certain number of bytes.

mb_strcut()正是这样做的,尽管您可能无法从几乎无法理解的文档中分辨出来。

关于php - 如何使用 PHP 查找 UTF-8 字符串中的字节数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2384260/

相关文章:

c++ - 将字符串截断为长度 N 的有效方法

python - WTForms 上的 UnicodeDecodeError

postgresql - 如何在 PostgreSQL 数据库中处理多种人类语言?

php - ActionScript [错误 #2036 : Load Never Completed] with dynamic generated images

php - 在 PHP 中外部化字符串的最佳实践

php - 维护用户列表 - MySQL/PHP

php - 如何在 PHP 中将 .xsd 文件转换为数组

java - 如何返回字符串中的数字循环? [Java]

javascript - 如何在线性时间内追加字符串

c - 使用 iconv() 的 UTF-8 到 C/POSIX 语言环境转换失败