这是一个非常奇怪的错误,我正试图修复它,但没有成功。我试图检查一个链接是否包含一个字符串:
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo $actual_link;产出如下:
http://xxx.xxx.xxx.xxx/plesk-site-preview/***********.com/xxx.xxx.xxx.xxx/然后:
if(strstr($actual_link,"plesk-site-preview") ){
echo"<meta name='robots' content='noindex'>";
}问题是strstr返回false,尽管子字符串plesk-site-preview包含在http://xxx.xxx.xxx.xxx/plesk-site-preview/***********.com/xxx.xxx.xxx.xxx/中。
如何纠正此错误?
编辑:
为了测试目的,我在if(strstr($actual_link,"plesk-site-preview") ){之前插入了以下一行:
$actual_link='http://xxx.xxx.xxx.xxx/plesk-site-preview/***********.com/xxx.xxx.xxx.xxx/';现在密码起作用了!在变量$actual_link处分配的字符串似乎在IF语句之前丢失了。
发布于 2018-09-15 21:40:36
文献资料说
string strstr ( string $haystack,混合型$needle,bool $before_needle = FALSE ) 返回部分干草堆字符串,从和包括第一次出现的针头到干草堆的末尾。 返回字符串的部分,如果找不到针,则返回FALSE。
而你的代码
if(strstr($actual_link,"plesk-site-preview")) 也许它应该是
if(strstr($actual_link,"plesk-site-preview") != "") 因为它返回一个字符串,如果成功,则返回布尔值。
嗯,实际上最好是
if(strstr($actual_link,"plesk-site-preview") !== FALSE)发布于 2018-09-15 21:45:50
如果需要检查字符串中是否存在子字符串,则可以使用斯特波斯,例如:
if(strpos($actual_link, "plesk-site-preview")){
echo"<meta name='robots' content='noindex'>";
}这种方式更好,因为strpos比strstr更快。
https://stackoverflow.com/questions/52349059
复制相似问题