Home >Backend Development >PHP Tutorial >Related analysis of php:similar_text function
When we want to compare the similarity or matching degree of certain sentences or content, we will inevitably use the similar_text() function. Here is a code demonstration of this function
I created 6 sentences, and Use symbols and then compare them in pairs. It can be seen that this function compares more than just letters. Other symbols can also be compared. Of course, you can also use percentages to compare the similarity of two sentences like the last statement in the code area. , the code is as follows:
$a ="hello,aa";
$b ="hello,bb";
echo "The first code demonstration:";
echo similar_text($a, $b );
$c ="helloaa";
$d ="hellobb";
echo "Second code demonstration:";
echo similar_text($c, $d);
echo "n";
$e = "fedcba";
$f ="abcdef";
echo "The third code demonstration";
echo similar_text($e, $f);
similar_text($c, $d,$percent);
echo "No. Four code demonstrations: ";
echo $percent;
?>
The output result is as follows:
At this time we will find a problem, which is the third code The area displays 1, and it is obvious that if the order is ignored, the output result should be 6. Regardless of the order, the result should be 0. How can it be 1? Let us further demonstrate:
$ e ="ab";
$f ="ba";
echo "The third code further demonstrates:";
similar_text($e, $f,$percent);
echo $percent."%";
? >
The result is:
Modify the parameters:
$e ="abcd";
$f ="dcba";
echo "The third code further demonstrates:";
similar_text($e, $f,$percent);
echo $percent."%";
?>
The result is |:
No matter how the proportion changes, there is always a similar character. , I checked the official literature and found that this function is implemented based on the algorithm in the book "Programming Classics: Implementing the World's Best Algorithms by Oliver (ISBN 0-131-00413-1)", but I still have no way to explain it clearly. If you have any opinions on this issue, please share it. Thank you
The above introduces the relevant analysis of the php:similar_text function, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.