wryan Posted December 13, 2010 Share Posted December 13, 2010 Hello, I am EXTREMELY new to PHP, and coding in general. I have attempted to search for an answer to this already, but I'm not sure I'm using terminology that will lead me to an solution, as I haven't come up with much. I'm hoping that someone can provide some assistance, or at least point me in the right direction for some reading. What I am trying to do it search a string for a match to any of 12 words. I'm wondering if there is a smarter way to do this than coding 12 different lines, each one looking for a specific word. If you can point me in the right direction, then thank you very much =) Link to comment https://forums.phpfreaks.com/topic/221533-search-string-for-any-match-to-a-list-of-words/ Share on other sites More sharing options...
Anti-Moronic Posted December 13, 2010 Share Posted December 13, 2010 You can use preg_match(): $string = 'here are a few words!'; if(preg_match("/(here|few|a)/i", $string)){ echo 'We found a match!'; } Separate your words with |, in the above: (here|few|a) Link to comment https://forums.phpfreaks.com/topic/221533-search-string-for-any-match-to-a-list-of-words/#findComment-1146754 Share on other sites More sharing options...
wryan Posted December 13, 2010 Author Share Posted December 13, 2010 I realized I should mention something. Depending on which word in the list is matched, I'd like to set a variable = to a different value (a string actually). Here's a mockup: String = "I'm at home" Word list: -home -work -dentist -prison -jupiter -lost do any of the words in the list show up in the string? If yes, then set string $Status equal to appropriate value for matched word (see below) home = "a" work = "b" dentist = "c" prison = "d" jupiter = "e" lost = "f" I hope that makes sense... Link to comment https://forums.phpfreaks.com/topic/221533-search-string-for-any-match-to-a-list-of-words/#findComment-1146756 Share on other sites More sharing options...
Anti-Moronic Posted December 13, 2010 Share Posted December 13, 2010 Ahh, something like this will work for that: $string = 'I am in prison'; $keywords = array('a'=>'home','b'=>'work','c'=>'dentist','d'=>'prison','e'=>'jupiter','f'=>'lost'); foreach($keywords as $wordstatus => $word){ if(preg_match('/'.$word.'/', $string)){ $status = $wordstatus; break; } } echo $status; You might be able to replace preg_match above with something else, and there are definitely other ways but I consider this one of the simplest. Link to comment https://forums.phpfreaks.com/topic/221533-search-string-for-any-match-to-a-list-of-words/#findComment-1146761 Share on other sites More sharing options...
wryan Posted December 13, 2010 Author Share Posted December 13, 2010 Thanks a million, Anti-Moronic! I'll start playing with that and see where I get =) Link to comment https://forums.phpfreaks.com/topic/221533-search-string-for-any-match-to-a-list-of-words/#findComment-1146764 Share on other sites More sharing options...
AbraCadaver Posted December 13, 2010 Share Posted December 13, 2010 If you're only looking for one value then one of these should work: $words = array('a' => 'home', 'b' => 'work'); //etc... $string = "I'm at home"; $status = key(array_intersect($words, explode(' ', $string))); $words = array('home' => 'a', 'work' => 'b'); //etc... $pattern = '/\b' . implode('|', array_keys($words)) . '\b/i'; $string = "I'm at home"; if(preg_match($pattern, $string, $matches)) { $status = $words[$matches[0]]; } Link to comment https://forums.phpfreaks.com/topic/221533-search-string-for-any-match-to-a-list-of-words/#findComment-1146772 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.