$bodytext = "we should see this text <more> but not this at all <html>";
if(stristr($bodytext, "<more>") == TRUE)
{
$find = "<more>";
$pos = stripos($bodytext, $find);
$bodytext = substr($bodytext, 0, $pos);
}
echo "$bodytext";
如果$ bodytext包含其他html代码,这也会导致上面的代码返回true:
<more
more>
如何仅(并且确切地)调整我的代码:
<more>
返回true?
答案 0 :(得分:2)
简单/轻松:
$bodytext = preg_replace('/(.*?)<more>.*/', $1, $bodytext);
答案 1 :(得分:2)
stristr返回匹配到字符串末尾的所有字符串。如果未找到匹配项,则返回false。
因此,您需要这样做:
if(stristr($bodytext, "<more>") !== false) {
// match found
}
stripos更适合您的需求:
$pos = stripos($bodytext, "<more>");
if($pos !== false) {
// match found
}
替代方案:请参阅Marc B的answer,它会执行您在单个声明中尝试实现的所有内容。
答案 2 :(得分:0)
您也可以使用explode函数并输出数组的第一个元素
$bodytext = "we should see this text <more> but not this at all <html>";
if(stristr($bodytext, "<more>") == TRUE)
{
$split = explode('<more>', $bodytext);
echo $split[0];
}