limitphp Posted November 19, 2008 Share Posted November 19, 2008 I have a variable $genres $genres can equal anything from one to many genres. They are seperated by commas and they have single quotes around all of them. example values of $genres: $genres = 'Jazz' or $genres = 'Jazz,'Blues','Rock','Folk','Indie' is there a way to detect if a word is in genre like Jazz? I'm going to need to check for all genres individually to see if they are in the array. The reason is because if they already selected it, I want the checkbox to keep it checked example: if($genres=='Jazz'){ echo "<input type='checkbox' name='genre' value='Jazz' CHECKED />";} else{ echo "<input type='checkbox' name='genre' value='Jazz' />";} Thanks Quote Link to comment https://forums.phpfreaks.com/topic/133370-solved-help-detecting-a-word-in-an-array/ Share on other sites More sharing options...
JonnoTheDev Posted November 19, 2008 Share Posted November 19, 2008 $lookingFor = "Jazz"; if(strstr($genres, $lookingFor)) { echo "Found: ".$lookingFor; } I would prefer to explode each item into an array as such (get rid of the quotes an leave the commas): $items = explode(",",$genres); $lookingFor = "Jazz"; if(in_array($lookingFor, $items)) { echo "Found: ".$lookingFor; } Quote Link to comment https://forums.phpfreaks.com/topic/133370-solved-help-detecting-a-word-in-an-array/#findComment-693673 Share on other sites More sharing options...
limitphp Posted November 19, 2008 Author Share Posted November 19, 2008 $lookingFor = "Jazz"; if(strstr($genres, $lookingFor)) { echo "Found: ".$lookingFor; } I would prefer to explode each item into an array as such (get rid of the quotes an leave the commas): $items = explode(",",$genres); $lookingFor = "Jazz"; if(in_array($lookingFor, $items)) { echo "Found: ".$lookingFor; } This is going to be on the index page and will be hit quite often. Do you know which one will be less taxing to the server? in_array or strstr? Quote Link to comment https://forums.phpfreaks.com/topic/133370-solved-help-detecting-a-word-in-an-array/#findComment-693679 Share on other sites More sharing options...
JonnoTheDev Posted November 19, 2008 Share Posted November 19, 2008 You want to use in_array() really and split your items into array elements. The strstr() function will also find substrings so it would find Jazz in both 'Jazz' & 'Jazzy' that may cause a problem. Neither of these will tax a server, you are talking microseconds! Quote Link to comment https://forums.phpfreaks.com/topic/133370-solved-help-detecting-a-word-in-an-array/#findComment-693681 Share on other sites More sharing options...
limitphp Posted November 19, 2008 Author Share Posted November 19, 2008 Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/133370-solved-help-detecting-a-word-in-an-array/#findComment-693721 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.