Mon*_*sha -1 php string function similarity output
我需要检查是否$string1是喜欢$string2但我不希望它是重要的,如果一个文本不同于另一个,如果它以相同的字符串开头.比如我想
if (admin.example2 == admin.example1)
Run Code Online (Sandbox Code Playgroud)
应该回归真实!最好的方法是什么?
我的确切要求.检查是否有任何字符串以admin开头的if条件存在于数组中.
if ($client_name like 'admin') {
...
...
}
Run Code Online (Sandbox Code Playgroud)
数组中有无限数量的条目.我只是想检查是否存在以"admin"开头的字符串是否存在.
好像你正在寻找similar_textPHP
<?php
similar_text("admin.example2", "admin.example1", $simpercentage);
if($simpercentage>90)
{
echo "Both strings are like ".round($simpercentage)."% similar.";
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT :
Both strings are like 93% similar.
Run Code Online (Sandbox Code Playgroud)
只需设置百分比(在上面的例子中,我将其设置为90%相似度),您可以根据需要进行调整.100%如果两个字符串都是admin.example2和admin.example2(如果两个字符串相同),代码将返回匹配
<?php
$arr=['admin1','administrator','admin2','I am an admin','adm','ADMIN'];
array_map(function ($v){ if(stripos(substr($v,0,5),'admin')!==false){ echo "$v starts with the text admin<br>";}},$arr);
Run Code Online (Sandbox Code Playgroud)
OUTPUT :
admin1 starts with the text admin
administrator starts with the text admin
admin2 starts with the text admin
ADMIN starts with the text admin
Run Code Online (Sandbox Code Playgroud)