Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check a partial similarity of two strings in PHP

Tags:

Is it any function in PHP that check the % of similarity of two strings?

For example i have:

$string1="Hello how are you doing"  $string2= " hi, how are you" 

and the function($string1, $string2) will return me true because the words "how", "are", "you" are present in the line.

Or even better, return me 60% of similarity because "how", "are", "you" is a 3/5 of $string1.

Does any function exist in PHP which do that?

like image 237
Ilya Libin Avatar asked May 13 '13 11:05

Ilya Libin


People also ask

How do I check if two strings are similar in PHP?

The strcmp() function compares two strings. Note: The strcmp() function is binary-safe and case-sensitive. Tip: This function is similar to the strncmp() function, with the difference that you can specify the number of characters from each string to be used in the comparison with strncmp().

How do you find the similarity between two strings?

Hamming Distance, named after the American mathematician, is the simplest algorithm for calculating string similarity. It checks the similarity by comparing the changes in the number of positions between the two strings.

Which function returns the number of matching character of two string in PHP?

The similar_text() function is a built-in function in PHP. This function calculates the similarity of two strings and returns the number of matching characters in the two strings.

Which function returns the number of matching character of two string?

The syntax of the strcmp() function is given below: In the above syntax, two parameters are passed as strings, i.e., str1 and str2, and the return type is int means that the strcmp() returns an integer value. The strcmp() function compares the character of both the strings.


2 Answers

As it's a nice question, I put some effort into it:

<?php $string1="Hello how are you doing"; $string2= " hi, how are you";  echo 'Compare result: ' . compareStrings($string1, $string2) . '%'; //60%   function compareStrings($s1, $s2) {     //one is empty, so no result     if (strlen($s1)==0 || strlen($s2)==0) {         return 0;     }      //replace none alphanumeric charactors     //i left - in case its used to combine words     $s1clean = preg_replace("/[^A-Za-z0-9-]/", ' ', $s1);     $s2clean = preg_replace("/[^A-Za-z0-9-]/", ' ', $s2);      //remove double spaces     while (strpos($s1clean, "  ")!==false) {         $s1clean = str_replace("  ", " ", $s1clean);     }     while (strpos($s2clean, "  ")!==false) {         $s2clean = str_replace("  ", " ", $s2clean);     }      //create arrays     $ar1 = explode(" ",$s1clean);     $ar2 = explode(" ",$s2clean);     $l1 = count($ar1);     $l2 = count($ar2);      //flip the arrays if needed so ar1 is always largest.     if ($l2>$l1) {         $t = $ar2;         $ar2 = $ar1;         $ar1 = $t;     }      //flip array 2, to make the words the keys     $ar2 = array_flip($ar2);       $maxwords = max($l1, $l2);     $matches = 0;      //find matching words     foreach($ar1 as $word) {         if (array_key_exists($word, $ar2))             $matches++;     }      return ($matches / $maxwords) * 100;     } ?> 
like image 98
Hugo Delsing Avatar answered Sep 30 '22 07:09

Hugo Delsing


As other answers have already said, you can use similar_text. Here's the demonstration:

$string1="Hello how are you doing" ; $string2= " hi, how are you";  echo similar_text($string1, $string2, $perc); //12  echo $perc; //61.538461538462 

will return 12, and will set in $perc the percentage of similarity as you asked for.

like image 20
Alex Siri Avatar answered Sep 30 '22 08:09

Alex Siri