Calculate two string similarity using PHP similar text, similartext_php tutorial

Source: Internet
Author: User
Tags first string

Calculates two string similarity using PHP similar text, Similartext


In web development, we often use PHP similar text to calculate two string similarity;

The use of 1,similar_text

If I want to calculate the similarity between "LY89CN" and "ly89", there are two ways to express it.

Copy the Code code as follows:
echo similar_text (' ly89cn ', ' ly89 ');

So output 4 because they have 4 characters equal

Copy the Code code as follows:
Similar_text (' ly89cn ', ' ly89 ', $percent);
Echo $percent;

So the output of the $percent represents a percentage, indicating that they have a similar degree of 80%

Of course, you can compare two Chinese characters, such as "Wang Yip Building's personal blog" and "Wang Yip Building"

Copy the Code code as follows:
Echo Similar_text (' Wang Yip Building's personal blog ', ' Wang Yip Building ');

So output 9, which means that their 9 bytes are equal

Copy the Code code as follows:
Similar_text (' Wang Yip Lou's personal blog ', ' Wang Yip Building ', $percent);
Echo $percent;

Output 54.545454545455, note Chinese characters may not be accurate!

PHP Similar_text () function

Instance

Calculates the similarity of two strings and returns the number of matching characters:

Copy the Code code as follows:
<?php
echo similar_text ("Hello World", "Hello Shanghai");
?>

Running an instance

Definition and usage

The Similar_text () function calculates the similarity of two strings.

The function can also calculate the percent similarity of two strings.

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

Copy the Code code as follows:
Similar_text (string1,string2,percent)

parameter description
String1 Necessary. Specifies the first string to compare.
string2 Necessary. Specifies a second string to compare.
Percent Optional. A variable name that specifies the similarity of the storage percentage.

Technical details

return value: Returns the number of matching characters for two strings.
PHP version: 4 +

More examples

Example 1

Calculates the percent similarity between two strings:

Copy the Code code as follows:
<?php
Similar_text ("Hello World", "Hello Shanghai", $percent);
Echo $percent. "%";
?>

http://www.bkjia.com/PHPjc/1068822.html www.bkjia.com true http://www.bkjia.com/PHPjc/1068822.html techarticle using PHP similar text to calculate the two string similarity, similartext in web development, we often use PHP similar text to calculate the two string similarity, 1,similar_text usage such as ...

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