Jump to content

What am I doing wrong on my search?


Jeffro

Recommended Posts

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

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) {

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! 

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.