Home>Article>Backend Development> Similar text function syntax and usage in PHP

Similar text function syntax and usage in PHP

墨辰丷
墨辰丷 Original
2018-06-06 10:56:46 2214browse

This article mainly introduces the syntax and usage of similar text function in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

1, usage of similar_text

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


## The code is as follows:

echo similar_text('ly89cn', 'ly89');

This outputs 4 because they have 4 equal characters



The code is as follows:

similar_text('ly89cn', 'ly89', $percent); echo $percent;

This outputs 80, $percent represents the percentage, description They are 80% similar

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('王业楼的个人博客', '王业楼'); 这样输出9,表示他们的9个字节数相等


The code is as follows:

similar_text('Wang Yelou's personal blog', 'Wang Yelou', $percent);
echo $percent;

Output 54.545454545455, please note that Chinese characters may not be accurate!

PHP similar_text() function

Example

Calculate the similarity of two strings and return the matching character Number:


The code is as follows:

Running example

Definition and usage

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

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

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

Grammar


The code is as follows:

similar_text(string1,string2,percent)

参数 描述
string1 必需。规定要比较的第一个字符串。
string2 必需。规定要比较的第二个字符串。
percent 可选。规定供存储百分比相似度的变量名。

Technical details

Return value: Returns the number of matching characters in the two strings.
PHP version: 4
##More examples

Example 1

Calculate the percentage similarity between two strings:


The code is as follows:

d33472d5d301945cfb63f9cd411c5143

Summary: The above is the summary of this article All content, I hope it will be helpful to everyone's study.

Related recommendations:

Five PHP methods for reading file content

PHP function to convert Chinese characters into pinyin

PHP jpgraph installation and basic usage

The above is the detailed content of Similar text function syntax and usage in PHP. For more information, please follow other related articles on the PHP Chinese website!

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