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
    Regular Coder
    Join Date
    Sep 2007
    Location
    Grahamstown, South Africa
    Posts
    237
    Thanks
    6
    Thanked 17 Times in 17 Posts

    Easy string matcher, commented.

    Hi guys I thought I would add a small function that I use for matching up strings . It is pretty useful after performing a search through a database to order it by relavancy. It uses a combination of similar_text() and levenshtein to make a powerful search tool.
    THE FUNCTION:
    PHP Code:
    <?php
    /*     $old_w = The array of words you want to match up to.
        $new_w = The word you want to check.
    */
    $result_array = array(); //creates an empty array
    function getSimilar($new_w,$old_w){
        global 
    $result_array//Make a global, because it (the variable,$result_array) is only defined in the function.
        
    foreach($old_w as $part){ //loop through the array of words.
            
    $sim similar_text($new_w,$part);     //run similar text on each word in the array.
            
    $lev levenshtein($new_w,$part);    //run levenshtein on each word in the array.
            
    if ($sim != 0){ //This part doesnt execute if sim = 0, as this means the word is not a match at all (no character matches).
                
    $lev_error intval(($lev / (strlen($part)) * 100)); //calculate the levenshtein percentage (error percentage)
                
    $per_sim intval($sim / (strlen($new_w))) * 100;     //calculate the similar text percentage
                
    $total $per_sim $lev_error//work out the total percentage
            
    } else{ //if sim = 1 then the total % match == 0
            
    $total 0;
            }
        
    $result_array[$part] = $total// add the total & word to the array.
        
    }
        
    arsort($result_array); //Sort the array with the highest match first
        
    return $result_array//return the value
    ?>
    CALLING THE FUNCTION:
    PHP Code:
    <?php
    $words 
    explode(",","lasse,manson,name,time,lassie,svend,svendie,jordan,grommit,plethora,tristan,noka,mark,ib,britte");
    /*     The above is an array of words that I want to match "lasse" up to. 
        A good use for this list of words is to search through a database
        and slap this result into this array */
    $name getSimilar("lasse",$words); //call the array
    print_r($result_array); //print the result
    ?>

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    You don't need $result_array outside of the function; you return it with the function anyway..
    PHP Code:
    <?php
    /*     $old_w = The array of words you want to match up to.
        $new_w = The word you want to check.
    */

    function getSimilar($new_w,$old_w){
        
    $result_array = array(); //creates an empty array
        
    foreach($old_w as $part){ //loop through the array of words.
            
    $sim similar_text($new_w,$part);     //run similar text on each word in the array.
            
    $lev levenshtein($new_w,$part);    //run levenshtein on each word in the array.
            
    if ($sim != 0){ //This part doesnt execute if sim = 0, as this means the word is not a match at all (no character matches).
                
    $lev_error intval(($lev / (strlen($part)) * 100)); //calculate the levenshtein percentage (error percentage)
                
    $per_sim intval($sim / (strlen($new_w))) * 100;     //calculate the similar text percentage
                
    $total $per_sim $lev_error//work out the total percentage
            
    } else{ //if sim = 1 then the total % match == 0
            
    $total 0;
            }
        
    $result_array[$part] = $total// add the total & word to the array.
        
    }
        
    arsort($result_array); //Sort the array with the highest match first
        
    return $result_array//return the value
    ?>
    PHP Code:
    <?php
    $words 
    explode(",","lasse,manson,name,time,lassie,svend,svendie,jordan,grommit,plethora,tristan,noka,mark,ib,britte");
    /*     The above is an array of words that I want to match "lasse" up to. 
        A good use for this list of words is to search through a database
        and slap this result into this array */
    $name getSimilar("lasse",$words); //call the array
    print_r($name); //print the result
    ?>


  •  

    Posting Permissions

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