Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I find a word in a sentence from the list of words in an array?

    I have few list of words (needle) I want to find in a sentence (haystack). After a word is found, how would I echo that word? Is there a way to have a multi array so:

    if the sentence contains word1,word2 or word3 then echo "coding"
    if the sentence contains word4,word5 or word6 then echo "forums"

    PHP Code:
    <?php
    $title_of_the_item 
    "This would be the sentence being pulled.";
    $words_from_title explode(" "$title_of_the_item);

    $word = array("structure""php""sentence""pulled");

    if(
    in_array($word,$words_from_title)){
    //even though two words are found in the array, use only 1
    }
    ?>
    Last edited by curb; 07-27-2007 at 09:22 PM.

  • #2
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Firstly I want to ask why do you want to break a string into array?
    If it is not really needed I think I would use this code:
    PHP Code:
    <?php
    $title_of_the_item 
    "This would be the sentence being pulled.";

    $word = array("structure""php""sentence""pulled");

    if((
    strpos($title_of_the_item,$word[1])||strpos($title_of_the_item,$word[2])||strpos($title_of_the_item,$word[3]))>){
        echo 
    "coding";
    } elseif ((
    strpos($title_of_the_item,$word[4])||strpos($title_of_the_item,$word[5])||strpos($title_of_the_item,$word[6]))>) {
        echo 
    "forums";
    }
    ?>
    I'm sure there are some shorter way but this was the first one I though about...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •