Jump to content

At a Loss: strstr


TheMayhem

Recommended Posts

I've spent two hours comparing strings.

 

$string1 = $gettext['$number'];

$string2 = $getstuff['url'];

if (strlen(strstr($string1,$string2))>0) {

print "YES<br />";

                                }

 

There is my code.

 

$string1 == aad.com

$string2 == aad.com

 

So why does it refuse to print out Yes? I am getting $string1 from an array that I created and $string2 from the database but all I want to see is $string2 has text in $string1 &/or it is identical. String1 is the haystack and String2 is the needle. I've tried every kind of tutorial possible but nothing is working.

Link to comment
https://forums.phpfreaks.com/topic/235897-at-a-loss-strstr/
Share on other sites

The Mayhem, try printing out the values you are about to compare using var_dump().  That might show what the problem is.  Alternatively you can print urlencode($string1) and urlencode($string2), which can also help to find differences in the strings.

 

Zurev's suggestion is not quite correct - it will return false if the string is found and it evaluates to 0.  What you have there with strlen() will work, but the most straightforward way is this

 

if (strstr($string1, $string2) !== false) {
    print "YES<br />";
}

 

strstr() will only return false if the string is not found, so this comparison will work 100% of the time.

Link to comment
https://forums.phpfreaks.com/topic/235897-at-a-loss-strstr/#findComment-1212679
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.