Php 不推荐使用:strpos():将来非字符串指针将被解释为字符串。使用显式chr()调用在中保留当前行为
嗨,有谁能帮我一把谢谢Php 不推荐使用:strpos():将来非字符串指针将被解释为字符串。使用显式chr()调用在中保留当前行为,php,Php,嗨,有谁能帮我一把谢谢 public function get_statement($t, &$contents) { // Locate the statement $tag_length = strlen($t['b']); $fpos = strpos($contents, $t['b']) + $tag_length; $lpos = strpos($contents, $t['e']); $length = $lpos - $
public function get_statement($t, &$contents)
{
// Locate the statement
$tag_length = strlen($t['b']);
$fpos = strpos($contents, $t['b']) + $tag_length;
$lpos = strpos($contents, $t['e']);
$length = $lpos - $fpos;
// Extract & return the statement
return substr($contents, $fpos, $length);
}
给出错误:已弃用:strpos():将来非字符串指针将被解释为字符串。使用显式的chr()调用来保留当前行为,并欢迎使用stackoverflow 根据: 不推荐将非字符串指针传递给字符串搜索函数 因此,从
PHP7.3
开始,当您调用strpos($haystack,$needle,$offset)
时,您需要确保第二个参数$needle
是一个字符串
在大多数情况下,您可以像这样将其强制转换为字符串strpos($haystack,(string)$needle,$offset)
回到您的代码示例,尝试以下方式修复它:
公共函数get_语句($t,&$contents)
{
//找到该语句
$tag_length=strlen($t['b']);
$fpos=strpos($contents,(string)$t['b'])+$tag_length;
$lpos=strpos($contents,(string)$t['e']);
$length=$lpos-$fpos;
//提取并返回语句
返回substr($contents,$fpos,$length);
}
有效的解决方案:
公共函数get_语句($t,&$contents)
{
//找到该语句
$tag_length=strlen($t['b']);
如果(!empty($t['b'])){
$fpos=strpos($contents,(string)$t['b'])+$tag_length;
}
如果(!empty($t['e'])){
$lpos=strpos($contents,(string)$t['e']);
}
$length=$lpos-$fpos;
//提取并返回语句
返回substr($contents,$fpos,$length);
}
您应该定义$t
包含的内容。^此外,您还应该说明您要归档的内容。您的代码片段出现了错误,但我们既不知道您的数据,也不知道您想要的输入与输出。我之前尝试过这一点,但得到了警告:strpos():空针输入,感谢您的帮助:)这意味着有时您会在此处得到null
或false
。就像解析某个内容一样,可能脚本没有找到它应该是什么,并将false
值传递给get\u语句
函数。所以问题不在这个函数中,我的答案没有区别。如果有差异-将代码作为答案而不是注释发布。如果没有差异-那么您选择我的答案作为未来用户的正确答案。好的,我发现,您检查If(!empty($t['b']){}