我试图使用strpos查找字符串是否存在,然后使用substr_replace删除该字符串.我的代码如下:
$title=$item->get_title();
$stringToFind='Help needed identifying this person in ';
if(strpos($title,$stringToFind)){
$title=substr_replace($title,'',0,strlen($stringToFind));
}
Run Code Online (Sandbox Code Playgroud)
但是,当我对strpos进行回声时,它总是返回false,应该是真的.所以我想知道strpos是否不做空白或其他什么?在哪种情况下有人会推荐一些东西?
你为什么不试一试:
$searchStr = 'Help needed identifying this person in ';
$title = str_replace($searchStr,'',$title);
Run Code Online (Sandbox Code Playgroud)
PHP文档将str_replace函数称为
用替换字符串替换所有出现的搜索字符串.
如果变量$searchStr中没有出现,$title则字符串将保持不变.
但是,如果它存在 - 它将被删除.您根本不需要测试它是否存在.如果您需要测试是否已进行更改,则可以使用strlen或mb_strlen根据编码比较两个字符串的长度.
输入/输出示例:
不变 - $searchStr = 'Help needed identifying this person in ';
// A match is found - string is changed
IN -> Help needed identifying this person in Timbuktu
OUT -> Timbuktu
IN -> Help needed identifying this person in Zimbabwe
OUT -> Zimbabwe
IN -> Help needed identifying this person in Netanya
OUT -> Netanya
// A match is not found - string remains the same
IN -> Stack Overflow is a programming Q & A site that’s free.
OUT -> Stack Overflow is a programming Q & A site that’s free.
IN -> We don’t run Stack Overflow. You do.
OUT -> We don’t run Stack Overflow. You do.
Run Code Online (Sandbox Code Playgroud)