A detailed description of the Similar_text and similarity Levenshtein functions for calculating string similarity in PHP
- $first = "ABCDEFG";
- $second = "AEG";
- Echo Similar_text ($first, $second); result output 3. If you want to display as a percentage, you can use its third parameter, as follows:
- $first = "ABCDEFG";
- $second = "AEG";
- Similar_text ($first, $second, $percent);
- Echo $percent;
- The code snippet is from: http://www.sharejs.com/codes/php/6094
Copy CodeThe use and implementation process of Similar_text function. The Similar_text () function is used primarily to calculate the number of matching characters for two strings, or to calculate the similarity of two strings (in percent). The Levenshtein () function we are going to introduce today is faster than the Similar_text () function. However, the Similar_text () function can provide more accurate results with fewer required modifications. Consider using the Levenshtein () function when the speed is less accurate and the string lengths are limited. Instructions for use First look at the description of the Levenshtein () function on 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 edit operations required between two strings, converted from one to another. Permission edits include replacing one character with another character, inserting a character, and deleting a character. For example, convert kitten to sitting: Sitten (K→s) Sittin (E→i) The sitting (→g) Levenshtein () function gives the same weight for each operation (replace, insert, and delete). However, you can define the cost of each operation by setting the optional Insert, replace, and delete parameters. Grammar: Levenshtein (String1,string2,insert,replace,delete) Parameter description ? string1 required. The first string to compare. ? string2 required. The second string to compare. ? insert is optional. The cost of inserting a character. The default is 1. ? replace is optional. The cost of replacing a character. The default is 1. ? Delete is optional. The cost of deleting a character. The default is 1. Hints and Notes ? If one of the strings exceeds 255 characters, the Levenshtein () function returns-1. The Levenshtein () function is not case sensitive. The Levenshtein () function is faster than the Similar_text () function. However, the Similar_text () function provides more precise results that require less modification. code example:
- Echo Levenshtein ("Hello World", "Ello World");
- echo "
";
- Echo Levenshtein ("Hello World", "Ello World", 10,20,30);
- ?>
Copy CodeOutput: 1 30
|