PHP returns a function of the distance between two strings Levenshtein ()

Source: Internet
Author: User
Tags first string

Instance

Calculates the Levenshtein distance between two strings:

<?phpecho Levenshtein ("Hello World", "Ello World"), echo "<br>", Echo Levenshtein ("Hello World", "Ello World", 10,20,30);? >

Definition and usage

The Levenshtein () function returns the Levenshtein distance between two strings.

Levenshtein distance, also known as edit distance, refers to the minimum number of editing operations required to convert a string into another string between two strings. Permission edits include replacing one character with another character, inserting a character, and deleting a character.

By default, PHP has the same weights 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.

Note: the Levenshtein () function is case insensitive.

Note: the Levenshtein () function is faster than the Similar_text () function. However, the Similar_text () function provides more precise results with fewer required modifications.

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.

Technical details

Return value: Returns the Levenshtein distance between two parameter strings and returns 1 if one of the strings exceeds 255 characters.

PHP version: 4.0.1+

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.
Example

<?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.