2014-09-10 118 views
0

例如,我有两个字符串:如何获得两个字符串之间最重复的子字符串?

$str1 = "one two three"; 

$str2 = "one two two three three three"; 

我怎样才能得到作为最终结果在上述情况下?

+0

'e检查手册xplode(“”,$ str1)'''explode(“”,$ str2)''然后使用数组操作函数 – Passerby 2014-09-10 09:34:40

+0

所有字符串都是由空格分隔的单词还是可以包含任何内容? – bumperbox 2014-09-10 09:34:42

+0

通过使用爆炸,字符串将被分开。 – XxXk5XxX 2014-09-10 09:38:08

回答

0

使用这段代码:

$arr = array_count_values(explode(" ", $yourstr)); 
asort($arr); 

reset($array); 

//get first val 
echo current($array), PHP_EOL ; 

//get first key 
echo key($array), PHP_EOL ; 

更多信息,请参阅:php manual

0

您可以使用爆炸,并把该值阵列。然后根据您创建的数组的索引获取您想要的值。

1

试试这个

$str1 = "one two three"; 

$str2 = "one two two three three three"; 

$array1=explode(" ",$str1); 
$array2=explode(" ",$str2); 
$result=array_merge($array1,$array2); 
$count=array_count_values($result); 
arsort($count); 
echo key($count); 
相关问题