Function code used to calculate string similarity in PHP. Similar_text-calculate the similarity of two strings intsimilar_text (string $ first, string $ second [, float $ secondaeg; echosimilar_text ($ first, $ second); result output 3. if you want to calculate similar_text-the similarity between the two strings
Int similar_text (string $ first, string $ second [, float & $ percent])
$ First is required. Specifies the first string to be compared.
$ Second is required. Specifies the second string to be compared.
$ Percent is optional. Specifies the name of the variable that stores the percentage similarity.
The similarity between the two strings is calculated based on the description of Oliver [1993. Note that this implementation does not use the stack in the Oliver virtual code, but is called recursively. This may cause the whole process to slow down or become faster. Note that the complexity of this algorithm is O (N ** 3), and N is the length of the longest string.
For example, we want to find the similarity between the string abcdefg and the string aeg:
The code is as follows:
$ First = "abcdefg ";
$ Second = "aeg ";
Echo similar_text ($ first, $ second); result output 3. if you want to display it as a percentage, you can use its third parameter, as shown below:
$ First = "abcdefg ";
$ Second = "aeg ";
Similar_text ($ first, $ second, $ percent );
Echo $ percent;
Use and implementation of the similar_text function. The similar_text () function is mainly used to calculate the number of matching characters of two strings. It can also calculate the similarity between two strings (expressed in percentages ). Compared with the similar_text () function, the levenshtein () function we will introduce today is faster. However, the similar_text () function provides more accurate results with fewer necessary modifications. The levenshtein () function can be used when the speed is less accurate and the string length is limited.
Instructions for Use
Let's take a look at the levenshtein () function instructions in the manual:
The levenshtein () function returns the Levenshtein distance between two strings.
Levenshtein distance, also known as the editing distance, refers to the minimum number of edits required to convert a string from one to another. Licensed editing operations include replacing one character with another, inserting one character, and deleting one character.
For example, convert kitten to sitting:
Sitten (k → s)
Sittin (e → I)
The sitting (→ g) levenshtein () function gives each operation the same weight (replacement, insertion, and deletion. However, you can set optional insert, replace, and delete parameters to define the cost of each operation.
Syntax:
Levenshtein (string1, string2, insert, replace, delete)
Parameter description
• String1 is required. The first string to be compared.
• String2 is required. The second string to be compared.
• Insert is optional. The cost of inserting a character. The default value is 1.
• Replace is optional. The cost of replacing a character. The default value is 1.
• Delete is optional. The cost of deleting a character. The default value is 1.
Tips and comments
• If one of the strings exceeds 255 characters, the levenshtein () function returns-1.
• The levenshtein () function is case insensitive.
• The levenshtein () function is faster than the similar_text () function. However, the similar_text () function provides more accurate results that require less modification.
Example
The code is as follows:
Echo levenshtein ("Hello World", "ello World ");
Echo"
";
Echo levenshtein ("Hello World", "ello World", 10, 20, 30 );
?>
Output: 1 30
Http://www.bkjia.com/PHPjc/326419.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326419.htmlTechArticlesimilar_text-calculate the similarity between two strings int similar_text (string $ first, string $ second [, float $ second = "aeg"; echo similar_text ($ first, $ second ); result output 3. if you want...