PHP 字符串不包含
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15498618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP string does not contain
提问by 10now
What I am trying to do is to display a message (1) if the current link does not contain the words "index" or "/?"
如果当前链接不包含单词“index”或“/?”,我想要做的是显示一条消息(1)。
I found this to do the direct opposite:
我发现这样做正好相反:
$page = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($page, 'index.php') !== false xor strpos($page, '/?') !== false) {
echo '1';
} else {
echo '2';
}
This code displays "2" on pages where there is no "index" or "/?" in the link, but I need the opposite: display "1" where there is no "index" or "/?" in the link.
此代码在没有“索引”或“/?”的页面上显示“2” 在链接中,但我需要相反的内容:在没有“索引”或“/?”的地方显示“1” 在链接中。
BTW I have tried all combinations: !strpos, TRUE, !==, but it doesn't seem to work for me. I need a way without the "else" in the code, otherwise I could just change up the echos.
顺便说一句,我已经尝试了所有组合:!strpos、TRUE、!==,但它似乎对我不起作用。我需要一种代码中没有“else”的方法,否则我只能改变回声。
回答by Stu
$page = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($page, 'index.php') === false && strpos($page, '/?') === false)
{
echo"1";
}
else
{
echo"2";
}
Should display 1if there's no index.phpor /?in $page
如果1没有index.php或/?在,则应显示$page
回答by tonystarck
$page = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($page, '/?') === false && strpos($page, 'index.php') === false)
{
echo "1";
}
else
{
echo "2";
}
回答by hjpotter92
The condition in your if statement is
if 语句中的条件是
if( <<COND 1>> xor <<COND 2>> )
where, xor's feature is
其中,xor的特征是
return
TRUEif either<<COND 1>>or<<COND 2>>isTRUE, but not both.
TRUE如果有<<COND 1>>或有<<COND 2>>则返回TRUE,但不能同时返回。
You should instead use this:
你应该改用这个:
if( strpos($page, 'index.php') !== false OR strpos($page, '/?') !== false )
Check this page for more information about logical operators.
查看此页面以获取有关逻辑运算符的更多信息。
回答by punit
Try this :
尝试这个 :
$page = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($page, 'index.php') === false || strpos($page, '/?') === false)
{
echo"1";
}
else
{
echo"2";
}

