refiking Posted July 19, 2009 Share Posted July 19, 2009 Simply put, I need to check to see if any of the values in the array are a string in the $results string. Here's what it looks like now: $options = array("one", "two","three", "four", "five"); if (strstr($results, $options)){ echo "Successful"; } Quote Link to comment https://forums.phpfreaks.com/topic/166477-solved-i-need-to-search-an-array-for-a-string/ Share on other sites More sharing options...
ngreenwood6 Posted July 19, 2009 Share Posted July 19, 2009 ok so whats the problem with that? What isnt working? Quote Link to comment https://forums.phpfreaks.com/topic/166477-solved-i-need-to-search-an-array-for-a-string/#findComment-877854 Share on other sites More sharing options...
Daniel0 Posted July 19, 2009 Share Posted July 19, 2009 Use in_array. Quote Link to comment https://forums.phpfreaks.com/topic/166477-solved-i-need-to-search-an-array-for-a-string/#findComment-877882 Share on other sites More sharing options...
refiking Posted July 19, 2009 Author Share Posted July 19, 2009 Still didn't work. Also tried in array and that didn't work, either. $results = "I am testing this right now"; $options = array("am", "yes", "check"); if (strstr($results, $options)) { echo "Successful"; } here is the other one: [code] $results = "I am testing this right now"; $options = array("am", "yes", "check"); if (in_array($results, $options)) { echo "Successful"; } Quote Link to comment https://forums.phpfreaks.com/topic/166477-solved-i-need-to-search-an-array-for-a-string/#findComment-878050 Share on other sites More sharing options...
Daniel0 Posted July 19, 2009 Share Posted July 19, 2009 So you want to check if it contains any of the words in the array? Then like this perhaps: $results = "I am testing this right now"; $options = array("am", "yes", "check"); $results = explode(' ', preg_replace('#[^a-z ]#', '', strtolower($results))); if (count(array_intersect($results, $options))) { echo 'Successful'; } Or like this: $results = "I am testing this right now"; $options = array("am", "yes", "check"); foreach ($options as $option) { if (preg_match('#\b' . preg_quote($option, '#') . '\b#i', $results)) { echo 'Successful'; break; } } Quote Link to comment https://forums.phpfreaks.com/topic/166477-solved-i-need-to-search-an-array-for-a-string/#findComment-878051 Share on other sites More sharing options...
refiking Posted July 19, 2009 Author Share Posted July 19, 2009 I managed to get the preg match to work. But, now I am getting duplicates. In my options array, I placed both the upper and lower case values. Is this what is causing that problem? for example... $options = array("web", "Web", "website", "Website"); Quote Link to comment https://forums.phpfreaks.com/topic/166477-solved-i-need-to-search-an-array-for-a-string/#findComment-878150 Share on other sites More sharing options...
.josh Posted July 19, 2009 Share Posted July 19, 2009 yes. Dan wrote the preg_match as case insensitive so you do not need both in your array. It would be better for you to only have a single version in your array and the preg_match will check for web Web WEB weB etc.. but if that is not possible (ex: if this array is being generated) then remove the i modifier in the preg_match if (preg_match('#\b' . preg_quote($option, '#') . '\b#', $results)) { Quote Link to comment https://forums.phpfreaks.com/topic/166477-solved-i-need-to-search-an-array-for-a-string/#findComment-878155 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.