2

在我看来,'strlen' 函数应该只返回字符串中的字符数。没有其他的。它确实如此,无论是计算 ASCII 字符还是 Unicode 字符。字符是一个字符,指向 ASCII 表或 UTF-8 表上的给定位置。而已。

无论出于何种原因,如果您想知道字符串的字节长度,那么您应该使用不同的函数。我是 PHP 脚本的新手,所以我还没有找到那个函数。(应该是类似 'bytelen()' 的东西?)

4

4 回答 4

1

是的,这将是最合乎逻辑的设计。然而,PHP 从一开始就没有计划支持多字节字符集。相反,它多年来一直在以一种混乱的方式发展。您已将您的问题标记为 PHP 4,但 PHP 5 还没有像样的 Unicode 支持(而且我认为它在不久的将来不会改变)。

无论如何,这有几个原因:

  • PHP 不是由企业规则控制的集中设计的公司拥有的闭源商业产品。

  • PHP 于 1995 年作为个人项目发布,由某个需要在他的静态主页中添加一些功能的人发布:当时它不需要 Unicode 支持。

  • 如果您修改诸如 strlen() 之类的核心函数,则必须以不破坏先前功能的方式进行。这并不容易。编写一个新的单独函数要容易得多。

更新

对不起,我忘记了你问题的第二部分。如果您需要处理 Unicode 字符串,则必须使用一组单独的函数:

您可能还会发现这些章节很有趣:

请注意您计划使用的每个功能所需的 PHP 版本;PHP 4 已经很老了。

于 2010-03-10T11:28:47.497 回答
1

如果我没有严重误解您,那么strlen() 就是您的“ bytelen() ”,正如此处其他回复中提到的那样。

strlen()本身不支持 utf-8 或其他多字节字符集;如果你想要一个合适的strlen(),你需要mb_strlen()

Pentium10 的函数 strBytes($str),如果你知道你的编码是 utf-8 并且由于某种原因你被 PHP4 的超低版本卡住了,从一瞥(不是测试)看起来它会是一个很好的选择。

(我确实建议您查看 Álvaro G. Vicario 的帖子,了解这种行为背后的原因。适当的原生 UTF-8 支持将随 PHP6 一起提供。)

于 2010-03-19T17:23:21.310 回答
1

mb_strlen()做你所追求的。

于 2010-03-10T11:07:19.173 回答
0
/** 
     * 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; 
    } 
于 2010-03-10T11:08:44.373 回答