Jeffro Posted August 22, 2011 Share Posted August 22, 2011 I've been trying to search my description for a phrase and if that phrase is found, I assign it to a variable. I found a "how to" with a google search and have been trying to create code from that example but it doesn't seem to be working. Anyone know why this might not work for me? Thanks! $description = "some random text with State: Texas in the description"; $texas = 'State: Texas'; $texassearch = strpos($description, $texas); if ($texassearch === true) { $state = "Texas"; } When I echo $state after doing this, it never changes. Thoughts? Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 22, 2011 Share Posted August 22, 2011 You are testing if the result of strpos() is the literal TRUE (by using three equal signs). strpos() returns the numerical index of where the string is found 0 - n or FALSE if the string is not found. Since 0 can be interpreted as FALSE you cannot just use a "normal" comparison (two equal signs). So you should check that the value is not a literal FALSE> if ($texassearch !== false) { Quote Link to comment Share on other sites More sharing options...
Jeffro Posted August 22, 2011 Author Share Posted August 22, 2011 You are testing if the result of strpos() is the literal TRUE (by using three equal signs). strpos() returns the numerical index of where the string is found 0 - n or FALSE if the string is not found. Since 0 can be interpreted as FALSE you cannot just use a "normal" comparison (two equal signs). So you should check that the value is not a literal FALSE> if ($texassearch !== false) { Thanks a ton. That did it! One last relative question.. I assume that searching this way will be case sensitive? What's the best way to make the search for $texas ignore case? Thanks again! Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 22, 2011 Share Posted August 22, 2011 Check the manual for strpos(). Then scroll down to the section for "See Also". http://php.net/manual/en/function.strpos.php Quote Link to comment Share on other sites More sharing options...
Jeffro Posted August 22, 2011 Author Share Posted August 22, 2011 Check the manual for strpos(). Then scroll down to the section for "See Also". http://php.net/manual/en/function.strpos.php Just found it before coming back here.. the wonderful stripos function. Thanks again all! Quote Link to comment 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.