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

Link to comment
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) {

 

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