英文:
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 = "";
$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;
}
答案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 < strlen($strRawText ?? '')) ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论