Php 使用strpos检查子字符串的存在性
我试图排除那些文本中没有Php 使用strpos检查子字符串的存在性,php,strpos,Php,Strpos,我试图排除那些文本中没有RT@的tweet。 这是我的密码: foreach ($tweets3 as $item) { $text = $item->text; $check = 'RT @'; $result = strpos($text, $check); if($result == false) continue; } 但这些推文
RT@
的tweet。
这是我的密码:
foreach ($tweets3 as $item)
{
$text = $item->text;
$check = 'RT @';
$result = strpos($text, $check);
if($result == false)
continue;
}
但这些推文也被排除在外
Mention text : RT @IBMcloud: A few #cloud highlights from the @IBM Annual Report. Read more: http://t.co/TJBHoX3vdU http://t.co/fG66SE7kV1
RT @holgermu: MyPOV - Nice way to put out our annual report in an interactive (engaging?) format - here is @IBM's - http://t.co/TIqi0soc5W
RT @TopixPolitix: Chinese State and Citizens Must Battle Airpocalypse Together http://t.co/nV5TGJG6Fl - http://t.co/cln83ufDnk
尽管他们的文本中有
RT@
。为什么?我认为你的逻辑颠倒了$result
将在找到文本时保留数值。您希望您的支票是:
if($result !== false)
continue;
请参见中的警告:
此函数可能返回布尔值FALSE,但也可能返回计算结果为FALSE的非布尔值。使用===运算符测试此函数的返回值
正如文档所述,strpos()
可以返回计算结果为布尔值的值FALSE
。例如,如果字符串开头有匹配项,strpos()
将返回0
为避免歧义,请始终使用严格比较(==
)而不是松散比较(=
)(只要可能):
使用
==
而不是=
。在PHP文档中,“此函数可能返回布尔值FALSE,但也可能返回计算结果为FALSE的非布尔值。有关详细信息,请阅读布尔值部分。使用===运算符测试此函数的返回值。”
foreach ($tweets3 as $item)
{
$text = $item->text;
$check = 'RT @';
$result = strpos($text, $check);
// if "RT @" text not found in tweet, skip to next iteration
if ($result === false) continue;
}