如何获得两个字符串之间最重复的子字符串?

时间:2014-09-10 09:32:12

标签: php

例如,我有两个字符串:

$str1 = "one two three";

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

在上述情况下,如何将作为最终结果?

4 个答案:

答案 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);

答案 1 :(得分: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

答案 2 :(得分:0)

您可以使用explode,并将值放在数组中。然后根据您创建的数组的索引获取所需的值。

答案 3 :(得分:0)

$words = "one two two three three three";
print_r( array_count_values(str_word_count($words, 1)) );

<强>输出

Array
(
    [one] => 1
    [two] => 2
    [three] => 3
)

检查手册 str_word_count array_count_values

相关问题