Depreciated strlen(): Passing null to parameter #1 ($string) of type string is depreciated

huangapple go评论60阅读模式
英文:

Depreciated strlen(): Passing null to parameter #1 ($string) of type string is depreciated

问题

PHP 8.1 升级后,在上述代码中显示了上面的警告信息。

有关如何使此代码与 PHP 8.1 兼容的任何想法吗?

private function cleanInput2($strRawText, $strAllowableChars, $blnAllowAccentedChars)
{
    $iCharPos = 0;
    $chrThisChar = "";
    $strCleanedText = "";

    // 基于可接受字符列表比较每个字符
    while ($iCharPos < strlen($strRawText))
    {
        // 仅包括有效字符 **
        $chrThisChar = substr($strRawText, $iCharPos, 1);
        if (strpos($strAllowableChars, $chrThisChar) !== FALSE)
        {
            $strCleanedText .= $chrThisChar;
        }
        elseif ($blnAllowAccentedChars == TRUE)
        {
            // 允许重音字符和大多数高位字节字符,这些是无害的 **
            if (ord($chrThisChar) >= 191)
            {
                $strCleanedText .= $chrThisChar;
            }
        }

        $iCharPos++;
    }

    return $strCleanedText;
}

请注意,我只进行了一些格式调整,没有进行与 PHP 8.1 特定的更改。如果您仍然遇到问题,可能需要查看警告信息以获取更多上下文,并对代码进行进一步的调整。

英文:

your text

The above warning is displayed in PHP 8.1 after upgrading from PHP 7.4

Any ideas on how this code can be changed for PHP 8.1 compatibility?

   private function cleanInput2($strRawText, $strAllowableChars, $blnAllowAccentedChars)
    {
        $iCharPos = 0;
        $chrThisChar = &quot;&quot;;
        $strCleanedText = &quot;&quot;;

        //Compare each character based on list of acceptable characters
        while ($iCharPos &lt; strlen($strRawText))
        {
            // Only include valid characters **
            $chrThisChar = substr($strRawText, $iCharPos, 1);
            if (strpos($strAllowableChars, $chrThisChar) !== FALSE)
            {
                $strCleanedText = $strCleanedText . $chrThisChar;
            }
            elseIf ($blnAllowAccentedChars == TRUE)
            {
                // Allow accented characters and most high order bit chars which are harmless **
                if (ord($chrThisChar) &gt;= 191)
                {
                    $strCleanedText = $strCleanedText . $chrThisChar;
                }
            }

            $iCharPos = $iCharPos + 1;
        }

        return $strCleanedText;
    }
   

答案1

得分: 1

请检查您的 $strRawText,确保在传递给 strlen() 之前不为null。这可以通过显式检查或添加类型提示来完成。

您也可以选择使用空值合并;这更加简洁。

while ($iCharPos < strlen($strRawText ?? '')) ...
英文:

You should check your $strRawText to ensure it's not null before passsing to strlen(). This can be done either with explicit check or adding typehint.

You can also alternatively use null coalescing; which is more concise.

while ($iCharPos &lt; strlen($strRawText ?? &#39;&#39;)) ...

huangapple
  • 本文由 发表于 2023年1月9日 04:51:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定