Home >Backend Development >PHP Tutorial >Code to compare similarity between two strings in php

Code to compare similarity between two strings in php

WBOY
WBOYOriginal
2016-07-25 08:43:131575browse

Detailed introduction to calculate string similarity similar_text and similarity levenshtein function in PHP

  1. $first = "abcdefg";
  2. $second = "aeg";
  3. echo similar_text($first, $second); result Output 3. If you want to display it in percentage, you can use its third parameter, as follows:
  4. $first = "abcdefg";
  5. $second = "aeg";
  6. similar_text($first, $second, $percent);
  7. echo $percent;
  8. //This code snippet comes from: http://www.sharejs.com/codes/php/6094
Copy code
Usage and implementation process of similar_text function. The similar_text() function is mainly used to calculate the number of matching characters in two strings, and can also calculate the similarity (in percentage) of two strings. The levenshtein() function we are going to introduce today is faster compared to the similar_text() function. However, the similar_text() function provides more accurate results with fewer modifications required. You can consider using the levenshtein() function when you are pursuing speed but less accuracy, and the string length is limited.

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:
  1. echo levenshtein("Hello World","ello World");
  2. echo "
    ";
  3. echo levenshtein("Hello World","ello World" ,10,20,30);
  4. ?>
Copy code
Output: 1 30
php


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn