How to match one long string phrase against another in PHP?

Discussion in 'PHP' started by sameerpanjwani, Sep 15, 2008.

  1. #1
    I am a bit confused on how to go about doing a match of one string against another. This is what I want to achieve:

    String 1: You have selected tea, biscuits and an ice-cream.

    String 2: The items available are "Drinks - tea, coffee"; "Snacks - Biscuits", "Dessert - Ice-cream"

    I want to match "String 1" against "String 2" and return "String 1" with all the matching words highlighted in yellow. So in this case, "String 1" would return with "tea", "biscuits" and "ice-cream" highlighted in yellow.

    How would I go about this?
     
    sameerpanjwani, Sep 15, 2008 IP
  2. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #2
    There might be some better way, but this is what I can think of:
    explode the second string, then do a preg_replace_all using this array elements, and string 1. Use span style="color:yellow">array element</span> to do the highlight.
    regards :)
     
    JEET, Sep 15, 2008 IP
  3. ASTURIAS

    ASTURIAS Member

    Messages:
    239
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #3
    explode() string 2 into an array and use a function to search for each word in string 1. Once that's performed, you can use another function to replace the matches with another code (HTML) that will be echoed with highlighted words (matches). :)
     
    ASTURIAS, Sep 15, 2008 IP
  4. sameerpanjwani

    sameerpanjwani Banned

    Messages:
    286
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ok I've made a mistake, string 2 is not necessarily in such an easy to decode format like mentioned above, it could be more like:

    String 2: The items available are "Drinks - tea, coffee"; "Snacks - Biscuits", "Dessert - Ice-cream"
     
    sameerpanjwani, Sep 15, 2008 IP
  5. ASTURIAS

    ASTURIAS Member

    Messages:
    239
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #5
    In that case you can loop many times and use explode() or trim()
     
    ASTURIAS, Sep 15, 2008 IP
  6. sameerpanjwani

    sameerpanjwani Banned

    Messages:
    286
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #6
    How would I explode the string without the quotes surrounding them, if i use explode using space as a parameter, the quotes still remain
     
    sameerpanjwani, Sep 15, 2008 IP
  7. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #7
    Use this to remove quotes:
    $str= str_replace('"','',$str);
    regards :)
     
    JEET, Sep 16, 2008 IP
  8. ASTURIAS

    ASTURIAS Member

    Messages:
    239
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #8
    Here you go!:D

    
    //Your first string, that will be checked for matches.
    $string1 = 'I hate your cat. Do you hate it?';
    //The possible words that will be matched.
    $string2 = '"hate,"cat",it"';
    //This removes the " ".
    $string2 = str_replace('"','',$string2);
    //Function to store the replacements.
    function text($obj) {
    $string1 = $obj;
    return $string1;
    }
    //
    $expo = explode(',',$string2);
    $howmany = count($expo);
    $i = 0;
    while($i <= $howmany) {
    $result = str_replace($expo[$i],'<span style="background-color:yellow;">'.$expo[$i].'</span>',$string1);
    $string1 = text($result);
    $i++;
    }
    //Results!
    print $result;
    
    Code (markup):
     
    ASTURIAS, Sep 16, 2008 IP