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?
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().
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.
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.
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.
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; } ?>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With