PHP stristr问题 $bodytext=“我们应该看到这个文本,但根本看不到这个”; if(stristr($bodytext,“”==TRUE) { $find=“”; $pos=stripos($bodytext,$find); $bodytext=substr($bodytext,0,$pos); } 回显“$bodytext”;

PHP stristr问题 $bodytext=“我们应该看到这个文本,但根本看不到这个”; if(stristr($bodytext,“”==TRUE) { $find=“”; $pos=stripos($bodytext,$find); $bodytext=substr($bodytext,0,$pos); } 回显“$bodytext”;,php,Php,如果$bodytext包含其他html代码,这也会导致上述代码返回true: $bodytext = "we should see this text <more> but not this at all <html>"; if(stristr($bodytext, "<more>") == TRUE) { $find = "<more>"; $pos = stripos($bodytext, $find); $bodyte

如果$bodytext包含其他html代码,这也会导致上述代码返回true:

$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";

如何调整我的代码,使其仅(准确地):


返回true?

简单/naive:

<more>
$bodytext=preg_replace('/(.*?.*/'),$1,$bodytext);
简单/naive:

<more>
$bodytext=preg_replace('/(.*?.*/'),$1,$bodytext);

您还可以使用explode函数输出数组的第一个元素

$bodytext = preg_replace('/(.*?)<more>.*/', $1, $bodytext);
$bodytext=“我们应该看到这个文本,但根本看不到这个”;
if(stristr($bodytext,“”==TRUE)
{
$split=分解(“”,$bodytext);
echo$split[0];
}

您还可以使用explode函数输出数组的第一个元素

$bodytext = preg_replace('/(.*?)<more>.*/', $1, $bodytext);
$bodytext=“我们应该看到这个文本,但根本看不到这个”;
if(stristr($bodytext,“”==TRUE)
{
$split=分解(“”,$bodytext);
echo$split[0];
}
返回从匹配到字符串末尾的所有字符串。如果未找到匹配项,则返回false

因此,您需要执行以下操作:

$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];
}
if(stristr($bodytext,”)!==false){
//找到匹配项
}
更适合您的需要:

if(stristr($bodytext, "<more>") !== false) {
    // match found
}
$pos=stripos($bodytext,”);
如果($pos!==false){
//找到匹配项
}
备选方案:请参阅Marc B's,它在一条语句中完成了您试图实现的所有功能。

返回从匹配到字符串结尾的所有字符串。如果未找到匹配项,则返回false

因此,您需要执行以下操作:

$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];
}
if(stristr($bodytext,”)!==false){
//找到匹配项
}
更适合您的需要:

if(stristr($bodytext, "<more>") !== false) {
    // match found
}
$pos=stripos($bodytext,”);
如果($pos!==false){
//找到匹配项
}
备选方案:参见Marc B's,它在一句话中完成了你似乎想要实现的一切