Home > Article > Backend Development > How to calculate the matching degree of two strings in PHP string learning
In the previous article "PHP String Learning: Comparing Two Strings", we introduced 3 methods of comparing two strings. If you are interested, you can take a look. In the following article, we will talk about the method of calculating the matching degree (similarity) of two strings in PHP. You can refer to it if you need it.
Calculate the matching degree (similarity) of two strings, that is, calculate the number of matching characters in the two strings; PHP has a built-in function that can implement various functions, that is, the similar_text() function, as follows Let's introduce this function.
First let’s look at a small example.
<?php header("Content-type:text/html;charset=utf-8"); $str1="Hello php!"; $str2="Hello java!"; echo "字符串 “'$str1'” 和字符串 “'$str2'” 的相似度为: ".similar_text($str1,$str2); ?>
Looking at this code, what do you think the result will be? Let's analyze it: There are 7 identical characters in the two strings: "Hello", space, and "!".
Then the similarity between the two strings should be "7", right? Let's take a look at the answer.
Yes, that's right. The similar_text() function counts the number of matching characters in the strings $str1
and $str2
, and returns the number of matching characters.
Let’s take a look at this function in detail.
similar_text() function calculates the similarity of two strings and returns the number of matching characters (in numerical form); it can also return the similarity of two strings in the form of percentage.
Look at its syntax format:
similar_text($string1,$string2,[$percent])
As you can see, the similar_text() function accepts 3 parameters: 2 required parameters $string1
and $ string2
, 1 omitted parameter $percent
. Through the above example, we know the meaning of the two required parameters. Let’s talk about the omitted parameters $percent
.
Parameter$percent
is the variable name used to specify the storage percentage similarity. The similar_text() function sets this parameter to return the similarity of two strings in the form of percentage.
Let’s take a look at the usage of parameter $percent
through code examples.
<?php header("Content-type:text/html;charset=utf-8"); $str1="Hello php!"; $str2="Hello java!"; similar_text($str1,$str2,$percent); echo "字符串 “'$str1'” 和字符串 “'$str2'” 的相似度为: ".$percent; ?>
Output results:
Using the similar_text() function, it can be used for fuzzy search functions, or other functions that require fuzzy matching . But because the processing rules for Chinese characters are different, the similar_text() function is not very inaccurate for Chinese calculations.
Okay, that’s all. If you want to know anything else, you can click this. → →php video tutorial
Finally, I recommend reading a classic course "PHP String Processing (Jade Girl Heart Sutra Edition)", it's free~ come and learn !
The above is the detailed content of How to calculate the matching degree of two strings in PHP string learning. For more information, please follow other related articles on the PHP Chinese website!