Home >Backend Development >PHP Tutorial >Code to compare similarity between two strings in php
Detailed introduction to calculate string similarity similar_text and similarity levenshtein function in PHP
Instructions for use First read the description of levenshtein() function in the manual: The levenshtein() function returns the Levenshtein distance between two strings. Levenshtein distance, also known as edit distance, refers to the minimum number of edit operations required between two strings to convert one into the other. Permitted editing operations include replacing one character with another, 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 equal weight to each operation (replacement, insertion and deletion). 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 optional. The cost of inserting a character. The default is 1. ?replace optional. The cost of replacing a character. The default is 1. ?delete optional. The cost of deleting a character. The default is 1. Tips and Notes ?The levenshtein() function returns -1 if one of the strings exceeds 255 characters. The ?levenshtein() function is not case sensitive. ?levenshtein() function is faster than similar_text() function. However, the similar_text() function provides more accurate results that require fewer modifications. Code example:
|