Home >Backend Development >PHP Tutorial >Similar text function syntax and usage in PHP
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
echo similar_text('ly89cn', 'ly89');This outputs 4 because they have 4 equal characters
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"
echo similar_text('王业楼的个人博客', '王业楼'); 这样输出9,表示他们的9个字节数相等
similar_text('Wang Yelou's personal blog', 'Wang Yelou', $percent);
echo $percent;
PHP similar_text() function
Example
Calculate the similarity of two strings and return the matching character Number:<?php echo similar_text("Hello World","Hello Shanghai"); ?>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. Grammarsimilar_text(string1,string2,percent)
参数 | 描述 |
---|---|
string1 | 必需。规定要比较的第一个字符串。 |
string2 | 必需。规定要比较的第二个字符串。 |
percent | 可选。规定供存储百分比相似度的变量名。 |
Technical details
Return value: | Returns the number of matching characters in the two strings. |
PHP version: | 4 |
d33472d5d301945cfb63f9cd411c5143
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!