drzfz9995 2018-09-15 21:34
浏览 259

PHP - strstr()返回false但$ haystack包含$ needle

This is a very strange error, i am trying to fix it without success. I am trying to check if a link contains a string:

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo $actual_link;

The output is:

http://xxx.xxx.xxx.xxx/plesk-site-preview/***********.com/xxx.xxx.xxx.xxx/

Then:

if(strstr($actual_link,"plesk-site-preview")  ){
echo"<meta name='robots' content='noindex'>";
}

The problem is that strstr return false despite the substring plesk-site-preview is contained in http://xxx.xxx.xxx.xxx/plesk-site-preview/***********.com/xxx.xxx.xxx.xxx/.

How can i fix this error?

EDIT:

I have inserted before if(strstr($actual_link,"plesk-site-preview") ){ the following line for testing purpose:

$actual_link='http://xxx.xxx.xxx.xxx/plesk-site-preview/***********.com/xxx.xxx.xxx.xxx/';

Now the code is working! It seems that the string assigned at the variable $actual_link is lost before the IF statement.

  • 写回答

2条回答 默认 最新

  • dpfad62426 2018-09-15 21:40
    关注

    The documentaion says

    string strstr ( string $haystack , mixed $needle [, bool $before_needle = FALSE ] )

    Returns part of haystack string starting from and including the first occurrence of needle to the end of haystack.

    Returns the portion of string, or FALSE if needle is not found.

    And you code

    if(strstr($actual_link,"plesk-site-preview")) 
    

    Perhaps it ought to be

    if(strstr($actual_link,"plesk-site-preview") != "") 
    

    as it returns a string, not a boolean if successful.

    Hmm, actually it would be better to

     if(strstr($actual_link,"plesk-site-preview") !== FALSE)
    
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?