如何获得两个字符串之间最重复的子字符串?
问题描述:
例如,我有两个字符串:如何获得两个字符串之间最重复的子字符串?
$str1 = "one two three";
$str2 = "one two two three three three";
我怎样才能得到作为最终结果在上述情况下?
答
使用这段代码:
$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
答
您可以使用爆炸,并把该值阵列。然后根据您创建的数组的索引获取您想要的值。
答
试试这个
$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);
答
$words = "one two two three three three";
print_r(array_count_values(str_word_count($words, 1)));
输出
Array
(
[one] => 1
[two] => 2
[three] => 3
)
'e检查手册xplode(“”,$ str1)'''explode(“”,$ str2)''然后使用数组操作函数 – Passerby 2014-09-10 09:34:40
所有字符串都是由空格分隔的单词还是可以包含任何内容? – bumperbox 2014-09-10 09:34:42
通过使用爆炸,字符串将被分开。 – XxXk5XxX 2014-09-10 09:38:08