Finding Instances Of Words In A String

Discussion in 'PHP' started by natekapi, May 4, 2007.

  1. #1
    I need to make something that will check a strings for any instances of words from a list, but I'm not really sure where to start. I see that I could use strstr for a single word, but I have a few words that I will need to look for so I'm not sure how to do that.

    Any help here would be greatly appreciated :)
     
    natekapi, May 4, 2007 IP
  2. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #2
    with strstr you can find first occurrence of an word in a given string , it's not accepting a list of words.

    you can try
    
    <?
    $string_to_check = "some string";
    $list_of_words = array('word1', 'word2');
    
    forearch ($list_of_words as $word) {
    
       if (preg_match("/".$word."/", $string_to_check)) {
    
           echo $word . " found";
    
       } else {
    
           echo $word . "not found";
       }
    
    }
    ?>
    
    PHP:
    you can try some variations of this simple code. :)
    happy phping...
     
    gibex, May 4, 2007 IP