string compare and suggestion? any script?

Discussion in 'PHP' started by mahmood, May 23, 2006.

  1. #1
    I am looking for a script that compares a string with an array of strings - form my database - and returns some suggestions.

    say for example a user enters "fod" and we have these words in our database:

    food
    fool
    fish
    hide
    mood
    god

    I expect this script to return "food" and "fool" and "god" and "mood"

    Anybody have seen such a thing?

    .
     
    mahmood, May 23, 2006 IP
    Will.Spencer likes this.
  2. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #2
    you can use either of the following code

    
    $kw="%foo%";
    $sql="Select * from table where field like '$kw'";
    
    Code (markup):
    or if you want it works like search engine....
    
    $kw="%foo search%";
    $sql="SELECT * FROM table WHERE MATCH (field1,field2,field3) AGAINST ('%$kw%')";
    
    Code (markup):
    Please take note the second option works only if your fields are in FULL TEXT and won't work if the the total row in your database is less than 4 and not work if you're trying to search word that less than 3 letter.
     
    PinoyIto, May 23, 2006 IP
    mahmood likes this.
  3. mahmood

    mahmood Guest

    Messages:
    1,228
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks PinoyIto
    I haven't tested yet but seems far better than what I expected.
     
    mahmood, May 23, 2006 IP
  4. mahmood

    mahmood Guest

    Messages:
    1,228
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well, it doesn't seem to be what I want. for example if the text in my field is "Radio" and I search for "Radios", it return 0 results. Perhaps it works with a large text and not a single word.
     
    mahmood, May 23, 2006 IP
  5. Lisper

    Lisper Guest

    Messages:
    86
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You can also look at the SOUNDEX statement, provided your database system provides that functionality. It matches words based on similarity in pronunciation. I used it with success in a project several years ago. I found a good explanation here:

    http://databases.about.com/od/development/l/aasoundex.htm
     
    Lisper, May 23, 2006 IP
  6. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #6
    You play the code

    this may work but slow for big database

    
    $kw="Radios";
    
    for($i=4;$i<=len($kw);$i++){
    $kw=substr($kw,0,$i);
    $kw="%$kw%";
    $sql="SELECT * FROM table WHERE MATCH (field1,field2,field3) AGAINST ('%$kw%')";
    
    }
    
    Code (markup):
     
    PinoyIto, May 23, 2006 IP