Use PHPsimilartext to calculate the similarity between two strings. similartext_PHP tutorial

Source: Internet
Author: User
PHPsimilartext is used to calculate the similarity between two strings, namely, similartext. PHPsimilartext is used to calculate the similarity between two strings. During website development, phpsimilartext is often used to calculate the similarity between two strings. 1. usage of similar_text, for example, using PHP similar text to calculate the similarity between two strings.

During website development, we often use php similar text to calculate the similarity between two strings;

1. usage of similar_text

If I want to calculate the similarity between "ly89cn" and "ly89", there are two representation methods.

The code is as follows:
Echo similar_text ('ly89cn', 'ly89 ');

In this way, output 4 because they have four equal characters.

The code is as follows:
Similar_text ('ly89cn', 'ly89 ', $ percent );
Echo $ percent;

In this way, 80, $ percent indicates the percentage, indicating that they have 80% similarity.

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

The code is as follows:
Echo similar_text ('Wang yelou's blog ', 'Wang yelou ');

In this way, 9 indicates that their 9 bytes are equal.

The code is as follows:
Similar_text ('Wang Yeou's blog ', 'Wang yelou', $ percent );
Echo $ percent;

Output 54.545454545455 characters. Note that Chinese characters may not be accurate!

PHP similar_text () function

Instance

Calculate the similarity between two strings and return the number of matching characters:

The code is as follows:
<? Php
Echo similar_text ("Hello World", "Hello Shanghai ");
?>

Running instances

Definition and usage

The similar_text () function computes the similarity between two strings.

This function can also calculate the percentage similarity between two strings.

Note: The levenshtein () function is faster than the similar_text () function. However, the similar_text () function provides more accurate results with fewer necessary modifications.

Syntax

The code is as follows:
Similar_text (string1, string2, percent)

Parameters Description
String1 Required. Specifies the first string to be compared.
String2 Required. Specifies the second string to be compared.
Percent Optional. Specifies the name of the variable that stores the percentage similarity.

Technical details

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

More instances

Example 1

Calculate the percentage similarity between two strings:

The code is as follows:
<? Php
Similar_text ("Hello World", "Hello Shanghai", $ percent );
Echo $ percent. "% ";
?>

Similar text calculates the similarity between two strings. similartext is often used in website development to calculate the similarity between two strings. 1. usage of similar_text is as follows...

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.