【问题标题】:PHP - strlen returning incorrect string length caused by quotePHP - strlen 返回由引号引起的错误字符串长度
【发布时间】:2013-09-26 09:46:31
【问题描述】:

我有以下来自数据库的字符串:Let's Get Functional

如果我通过 strlen 运行它,它会返回 25 个字符而不是预期的 20 个字符。var 转储显示字符串看起来像上面那样(没有 html 引用等)。

如果我删除单引号 strlen 返回 19 个字符。

很明显,引号返回的是 5 个字符而不是 1 - 为什么?我怎样才能阻止这种情况?

谢谢!

【问题讨论】:

  • 显示您的两个var_dump 代码(带有结果)
  • 你能粘贴你使用的代码吗?
  • 带引号:字符串(25)“让我们发挥作用”
  • 不加引号:string(19) "Lets Get Functional"
  • 请在此处粘贴您的代码:codepad.org

标签: php strlen


【解决方案1】:

ASCII 的实体名称是 ',对于 ',等于 5 个字符:ASCII Table

问题似乎与您的' 被评估,而不仅仅是被读取的事实有关。 试着让你的字符串像这样:

$myString = 'Hello World!';

不是这样的:

$myString = "Hello World!";

它读取并评估所有字符串的内容,而不仅仅是读取,因此将您的特殊字符解释为它们的 ASCII 代码。

PHP 手册说:If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters

我认为您的strlen() 函数是使用包含" 的参数调用的,因此它给出的是评估的strlen(),而不是读取的。

试试这个:

$countChars = strlen(utf8_decode($myString));

utf8_decode() converts characters that are not in ISO-8859-1 to '?', which, for the purpose of counting, is quite alright.

查看this,了解有关单引号和双引号之间差异的更多信息。

【讨论】:

    【解决方案2】:

    正如@deformhead 已经解释的那样,您的撇号似乎已转换为HTML ' 字符串。我的猜测是,在将字符串从数据库中取出并在其上调用 strlen() 之间,您会在中间的某个位置调用 htmlentities()。

    您还可以使用 CHAR_LENGTH() (MySQL) 在选择查询中检查从数据库中获取的字符数。

    您可能会考虑的另一个问题是 strlen() 不适用于多字节字符,因此如果您将使用非 ASCII 字符,那么您最好使用具有正确编码的 mb_strlen()。但是,这种情况无法解释结果中 5 个字符的差异(strlen() 计算字节数而不是字符串中的字符数)。

    希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      不可能。

      <?php
      $str = "Let's Get Functional";
      echo strlen($str), "\n"; // 20
      

      Look at code output here.

      如何调试?

      打印每个字符的ASCII码:

      $str = "Let's Get Functional";
      $len = strlen($str);
      for ($i = 0; $i < $len; $i++)
      {
          echo "$i\t", ord($str[$i]), "\n";
      }
      

      这是结果:

      0   L       76
      1   e       101
      2   t       116
      3   '       39
      4   s       115
      5           32
      6   G       71
      7   e       101
      8   t       116
      9           32
      10  F       70
      11  u       117
      12  n       110
      13  c       99
      14  t       116
      15  i       105
      16  o       111
      17  n       110
      18  a       97
      19  l       108
      

      【讨论】:

        【解决方案4】:
        <?php
        
        $string = "Let's Get Functional";
        
        echo strlen($string);
        
        ?>
        

        此代码返回 20 个字符。

        【讨论】:

        • 是的,谢谢。我知道它应该返回 20,但它返回 25(我在原始问题中说过)。 CMS 可能添加了一些奇怪的编码或字符,我正在尝试找出原因以便消除它们。
        • 您从未在问题中提及任何有关任何 CMS 的内容。我们只能根据您提出的问题提供答案。您使用的是什么 CMS,该代码在该 CMS 中的什么位置?难道 $string 已经在别处设置了吗?为什么不尝试通过使用另一个变量来查看它是否有效。例如$myString = "Let's Get Functional";
        【解决方案5】:

        我遇到了和你一样的问题,也许这会对某人有所帮助。

        单引号被转换为“& #39;”这给了我不正确的结果。 简单地用单引号替换字符串就解决了我的问题。

        $string = "让我们变得实用"; //来自POST或数据库的字符串

        回声 strlen($string); //25

        $string = str_replace("'", "'",$string); 回声 strlen($string); //20

        【讨论】:

          猜你喜欢
          • 2011-05-08
          • 2014-07-22
          • 1970-01-01
          • 2021-01-12
          • 1970-01-01
          • 1970-01-01
          • 2016-05-18
          • 2016-07-13
          • 2018-09-10
          相关资源
          最近更新 更多