Stuart_Westgate Posted February 10, 2013 Share Posted February 10, 2013 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/274293-how-do-a-look-for-a-word-within-a-string/ Share on other sites More sharing options...
kicken Posted February 10, 2013 Share Posted February 10, 2013 Use strpos to search for the value in the string. Be sure to read the docs, particularly the warning section. Quote Link to comment https://forums.phpfreaks.com/topic/274293-how-do-a-look-for-a-word-within-a-string/#findComment-1411523 Share on other sites More sharing options...
requinix Posted February 10, 2013 Share Posted February 10, 2013 And the case-insensitive counterpart to strpos() is stripos. Quote Link to comment https://forums.phpfreaks.com/topic/274293-how-do-a-look-for-a-word-within-a-string/#findComment-1411598 Share on other sites More sharing options...
Stuart_Westgate Posted February 11, 2013 Author Share Posted February 11, 2013 (edited) 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. Edited February 11, 2013 by Stuart_Westgate Quote Link to comment https://forums.phpfreaks.com/topic/274293-how-do-a-look-for-a-word-within-a-string/#findComment-1411679 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.