TheMayhem Posted May 9, 2011 Share Posted May 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235897-at-a-loss-strstr/ Share on other sites More sharing options...
Zurev Posted May 9, 2011 Share Posted May 9, 2011 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 />"; } Quote Link to comment https://forums.phpfreaks.com/topic/235897-at-a-loss-strstr/#findComment-1212665 Share on other sites More sharing options...
btherl Posted May 9, 2011 Share Posted May 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235897-at-a-loss-strstr/#findComment-1212679 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.