Jump to content

Search string for any match to a list of words?


wryan

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.