当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > similar_text() 函数
The similar_text() function returns the number of matching characters of two strings.
similar_text()函数的作用是:返回两个字符串中相匹配的字符数量。
It can also calculate the similarity of the two strings in percent.
它同样可以计算两个字符串的相似性(用“百分比%”来表示)。
similar_text(string1,string2,percent) |
Parameter参数 | Description描述 |
---|---|
string1 | Required. Specifies the first string to be compared 必要参数。指定参与比较的第一个字符串对象 |
string2 | Required. Specifies the second string to be compared 必要参数。指定参与比较的第二个字符串对象 |
percent | Optional. Specifies a variable name for storing the similarity in percent 可选参数。指定一个用来存储相似性的变量名(用“百分比%”来表示) |
Note: The levenshtein() function is faster than the similar_text() function. However, the similar_text() function will give you a more accurate result with less modifications needed.
注意:levenshtein()函数的运行速度要快于similar_text()函数。然而,similar_text()函数将会给出相对于levenshtein()函数而言更精确的结果,并且你无需对其进行过多修改。
<?php echo similar_text("Hello World","Hello Peter"); ?> |
The output of the code above will be:
上述代码将输出下面的结果:
7 |
<?php similar_text("Hello World","Hello Peter",$percent); echo $percent; ?> |
The output of the code above will be:
上述代码将输出下面的结果:
63.6363636364 |