Calculates two string similarity using PHP similar text, Similartext
In web development, we often use PHP similar text to calculate two string similarity;
The use of 1,similar_text
If I want to calculate the similarity between "LY89CN" and "ly89", there are two ways to express it.
Copy the Code code as follows:
echo similar_text (' ly89cn ', ' ly89 ');
So output 4 because they have 4 characters equal
Copy the Code code as follows:
Similar_text (' ly89cn ', ' ly89 ', $percent);
Echo $percent;
So the output of the $percent represents a percentage, indicating that they have a similar degree of 80%
Of course, you can compare two Chinese characters, such as "Wang Yip Building's personal blog" and "Wang Yip Building"
Copy the Code code as follows:
Echo Similar_text (' Wang Yip Building's personal blog ', ' Wang Yip Building ');
So output 9, which means that their 9 bytes are equal
Copy the Code code as follows:
Similar_text (' Wang Yip Lou's personal blog ', ' Wang Yip Building ', $percent);
Echo $percent;
Output 54.545454545455, note Chinese characters may not be accurate!
PHP Similar_text () function
Instance
Calculates the similarity of two strings and returns the number of matching characters:
Copy the Code code as follows:
<?php
echo similar_text ("Hello World", "Hello Shanghai");
?>
Running an instance
Definition and usage
The Similar_text () function calculates the similarity of two strings.
The function can also calculate the percent similarity of two strings.
Note: the Levenshtein () function is faster than the Similar_text () function. However, the Similar_text () function provides more precise results with fewer required modifications.
Grammar
Copy the Code code as follows:
Similar_text (string1,string2,percent)
parameter |
description |
String1 |
Necessary. Specifies the first string to compare. |
string2 |
Necessary. Specifies a second string to compare. |
Percent |
Optional. A variable name that specifies the similarity of the storage percentage. |
Technical details
return value: |
Returns the number of matching characters for two strings. |
PHP version: |
4 + |
More examples
Example 1
Calculates the percent similarity between two strings:
Copy the Code code as follows:
<?php
Similar_text ("Hello World", "Hello Shanghai", $percent);
Echo $percent. "%";
?>
http://www.bkjia.com/PHPjc/1068822.html www.bkjia.com true http://www.bkjia.com/PHPjc/1068822.html techarticle using PHP similar text to calculate the two string similarity, similartext in web development, we often use PHP similar text to calculate the two string similarity, 1,similar_text usage such as ...