Home  >  Article  >  Backend Development  >  字符串匹配问题:求大神指导。

字符串匹配问题:求大神指导。

WBOY
WBOYOriginal
2016-06-23 13:34:151103browse

$a="1,2,3,4,5"
$b="1,2,3,4,6"
$c="1,2,4,5,6"
$d="2,3,4,5,6"
$f="7,8,9,10,11"
如何判断出$b,$c,$d,$f和$a的相似度呢?然后,判断出他们的百分比。


回复讨论(解决方案)

快来帮帮忙啊

前提:
1.都能以,拆分
2.拆分后的数量必须相同
3.不进行顺序比较

function similarity($a,$b){	$a_arr=explode(',',$a);	$b_arr=explode(',',$b);	$num=count(array_intersect($a_arr,$b_arr));	$count=count($a_arr);	return ($num/$count*100).'%';}$a="1,2,3,4,5";$b="1,2,3,4,6";$c="1,2,4,5,6";$d="2,3,4,5,6";$f="7,8,9,10,11";echo similarity($a,$b).'<br>';echo similarity($a,$c).'<br>';echo similarity($a,$d).'<br>';echo similarity($a,$f).'<br>';

谢谢啦,昨天晚上我也找到这个函数了。

php自带一个类似的,我想从数据库中提取用户的选择然后分别对其他用户匹配。嘿嘿

Statement:
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