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

Similar text function syntax and usage in PHP

墨辰丷
Release: 2023-03-30 06:00:01
Original
2310 people have browsed it

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');
Copy after login

This outputs 4 because they have 4 equal characters



The code is as follows:

similar_text('ly89cn', 'ly89', $percent);
echo $percent;
Copy after login

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个字节数相等
Copy after login


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:

<?php
echo similar_text("Hello World","Hello Shanghai");
?>
Copy after login

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)
Copy after login

参数 描述
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:

similar_text("Hello World","Hello Shanghai",$percent);
echo $percent. "%";
?>

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template