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? Link to comment https://forums.phpfreaks.com/topic/245430-what-am-i-doing-wrong-on-my-search/ 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) { Link to comment https://forums.phpfreaks.com/topic/245430-what-am-i-doing-wrong-on-my-search/#findComment-1260540 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! Link to comment https://forums.phpfreaks.com/topic/245430-what-am-i-doing-wrong-on-my-search/#findComment-1260560 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 Link to comment https://forums.phpfreaks.com/topic/245430-what-am-i-doing-wrong-on-my-search/#findComment-1260589 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! Link to comment https://forums.phpfreaks.com/topic/245430-what-am-i-doing-wrong-on-my-search/#findComment-1260616 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.