Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 = "";
        $strCleanedText = "";

        //Compare each character based on list of acceptable characters
        while ($iCharPos < 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) >= 191)
                {
                    $strCleanedText = $strCleanedText . $chrThisChar;
                }
            }

            $iCharPos = $iCharPos + 1;
        }

        return $strCleanedText;
    }
   
like image 235
Andy Avatar asked Oct 24 '25 08:10

Andy


1 Answers

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 < strlen($strRawText ?? '')) ...
like image 111
Ammar Avatar answered Oct 26 '25 23:10

Ammar