Code used to calculate string similarity in PHP

Source: Internet
Author: User
Tags first string

Similar_text-calculate the similarity between 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:

Copy codeThe 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

Copy codeThe Code is as follows: <? Php
Echo levenshtein ("Hello World", "ello World ");
Echo "<br/> ";
Echo levenshtein ("Hello World", "ello World", 10, 20, 30 );
?>

Output: 1 30

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.