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 =) Quote Link to comment 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) Quote Link to comment 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... Quote Link to comment 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. Quote Link to comment 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 =) Quote Link to comment 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]]; } Quote Link to comment 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.