PHP - strstr() returns false but the $haystack contains the $needle

3807 views php

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:***********.com/


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***********.com/

How can i fix this error?

answered question

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

@NineBerry i have inserted your code but the error remains! Please note that my code is exact and there are not further lines of code between echo $actual_link; and the IF statement.

2 Answers


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


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)

posted this

If you need check string presence of a substring in a string then you can use strpos, for example:

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

This way is better, because strpos is faster than strstr

posted this

Have an answer?


Please login first before posting an answer.