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
Share on other sites

Why are you doing strlen? strstr returns the portion of the match, which would eval true, or false if it can't find it, so just do:

 

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

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.