Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecate

后端开发   发布日期:2023年05月16日   浏览次数:274

PHP程序界面报错:Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in。

意思是:不推荐使用:str_replace():不推荐将null传递给数组| string类型的参数#3($subject)

前后翻了几遍程序,找到了涉及到的代码段:

function keyword_replace($keyword){
	$search_nohave   = array(",","/", "\\", ".", ";", ":", "\"", "!", "~", "`", "^", "(", ")", "?", "-", "\t", "\n", "'", "<", ">", "\r", "\r\n", "$", "&", "%", "#", "@", "+", "=", "{", "}", "[", "]", ":", ")", "(", ".", "。", ",", "!", ";", "“", "”", "‘", "’", "[", "]", "、", "―", " ", "-", "…");
	foreach ($search_nohave as $search_rep){
		$keyword = str_replace($search_rep,"",$keyword); 
	}
	return $keyword;
}

出现的问题是因为传进来的$keyword变量传为空,所以报错,但这个错误不是致命的不影响程序运行,如果不是强迫症可以忽略。

修改后可以解决问题,主要是判断一下传进来的参数是不是空,代码如下:

function keyword_replace($keyword){
	if ($keyword!=''){
		$search_nohave   = array(",","/", "\\", ".", ";", ":", "\"", "!", "~", "`", "^", "(", ")", "?", "-", "\t", "\n", "'", "<", ">", "\r", "\r\n", "$", "&", "%", "#", "@", "+", "=", "{", "}", "[", "]", ":", ")", "(", ".", "。", ",", "!", ";", "“", "”", "‘", "’", "[", "]", "、", "―", " ", "-", "…");
		foreach ($search_nohave as $search_rep){
			$keyword = str_replace($search_rep,"",$keyword); 
		}
		return $keyword;
	}
}


以上就是Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecate的详细内容,更多关于Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecate的资料请关注九品源码其它相关文章!