In website development, we often use php similar text to calculate the similarity of two strings;
1, usage of similar_text
If I want to calculate the similarity between "ly89cn" and "ly89", there are two ways to express it
Copy code The code is as follows:
echo similar_text('ly89cn', 'ly89');
This outputs 4 because they have 4 equal characters
Copy code The code is as follows:
similar_text('ly89cn', 'ly89', $percent);
echo $percent;
This outputs 80, $percent represents the percentage, indicating that they are 80% similar
Of course, you can also compare two Chinese characters, such as "Wang Yelou's personal blog" and "Wang Yelou"
Copy code The code is as follows:
echo similar_text('Wang Yelou's personal blog', 'Wang Yelou');
Output 9 like this, indicating that their 9 bytes are equal
Copy code 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 between two strings and return the number of matching characters:
Copy code The code is as follows:
echo similar_text("Hello World","Hello Shanghai");
?>
Running instance
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
Copy code The code is as follows:
similar_text(string1,string2,percent)
Parameters | Description |
---|---|
string1 | Required. Specifies the first string to compare. |
string2 | Required. Specifies the second string to be compared. |
percent | Optional.规定供存储百分比相似度的变量名。 |
技术细节
返回值:
|
返回两个字符串的匹配字符的数目。
|
||||
PHP 版本: | 4
更多实例
例子 1
similar_text("Hello World","Hello Shanghai",$percent); echo $percent. "%"; ?> http://www.bkjia.com/PHPjc/1068822.htmlwww.bkjia.comtrue http://www.bkjia.com/PHPjc/1068822.htmlTechArticle 使用PHP similar text计算两个字符串相似度,similartext 在网站开发中,我们经常使用php similar text 计算两个字符串相似度; 1,similar_text的用法 如...
|