Search
 
SCRIPT & CODE EXAMPLE
 

PHP

fuzzy search in php with percentage

function wordSimilarity($s1,$s2) {

    $words1 = preg_split('/s+/',$s1);
    $words2 = preg_split('/s+/',$s2);
    $diffs1 = array_diff($words2,$words1);
    $diffs2 = array_diff($words1,$words2);

    $diffsLength = strlen(join("",$diffs1).join("",$diffs2));
    $wordsLength = strlen(join("",$words1).join("",$words2));
    if(!$wordsLength) return 0;

    $differenceRate = ( $diffsLength / $wordsLength );
    $similarityRate = 1 - $differenceRate;
    return $similarityRate;

}

// This function gives you a floating point value between 0 and 1 where 1 is total similarity.
// Let's see some tests

  $test = "this is something you've never done before";
    
  wordSimilarity($test,"this is something you've never done before");  //  1.000
  wordSimilarity($test,"this is something");                           //  0.588
  wordSimilarity($test,"this is nothing you have ever done");          //  0.312
  wordSimilarity($test,"leave me alone with lorem ipsum");             //  0.000
  wordSimilarity($test,"before you do something you've never done");   //  0.845
  wordSimilarity($test,"never have i ever done this");                 //  0.448

Comment

PREVIOUS NEXT
Code Example
Php :: array_key_first not works 
Php :: php usort two columns 
Php :: Drupal 9 loop term objects to retrieve term data (id, name, uuid) 
Php :: php if 2 files in dir unlink the olderst 
Php :: magento 2.4.3 how to log 
Php :: verify that a valid login cookie was sent in order to do special things for that logged-in 
Php :: how to check my server use cgi, fcgi or fpm. 
Php :: wordpress how to string multple function.php files together 
Php :: Collapse all codes in PHP Storm IntelliJ 
Php :: php clear echo messages 
Php :: HTTP 500 ERROR WITHOUT MESSAGE PHP 
Php :: get current tax page 
Php :: @parent laravel 
Php :: post with count greater than 1 laravel 
Php :: laravel relationship hasmany 
Php :: PHP include causes white space at the top of the page 
Php :: Stopping On First Validation Failure 
Php :: how to restrict user to some pages using php 
Php :: woo variable product get field 
Php :: app/Controllers/Home.php 
Php :: php make text id attribute safe 
Php :: pass variable in translation larvel 
Php :: "A non well formed numeric value encountered 
Php :: call node js jquery http php 
Php :: css en linea php 
Php :: if ip in the array redirect php 
Php :: configurar pagina html php para mobile 
Php :: Random select value on array factory Laravel 
Php :: how to verify envato purchase code in php 
Php :: wordpress profile queries 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =