Jump to content

How do a look for a word within a string?


Stuart_Westgate

Recommended Posts

I want to look for the word "pant" within this string: "Pants: I'm going to be late!"

 

How would I do this?

 

I'd prefer it if I wasn't case sensitive search!

 

This is how I'm trying to do it at the moment but it's not working!

 

$status = "Pants: I'm going to be late!";

 

$changeValue = strtok($status, " ");

$changeValue = strtolower($changeValue);

 

$imageValue = "images/";

 

if($changeValue == "pant:"){ // if I change this value to "pants:" with the colon it works however I only want to search for the word "pant" without the "s" or the ":"

$imageValue .= "pant.PNG";

}

stripos is defo the best way of doing it. Thanks buddy.

 

This was my code for anyone else that was stuck:

 

if(stripos($changeValue, $string) !== false){

echo "The string contains the value of the string variable!";

}

 

 

And the case-insensitive counterpart to strpos() is stripos.

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.