本文介绍了如何获取字符串中相同字符的最长序列的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要指定:
function getLength($str)
{
//function i need
}
$str1 = 'aabbcccc';
$str2 = 'aabbccccaaaaa';
echo getLength($str1); //will get 4
echo getLength($str2); //will get 5
任何好的想法?
推荐答案
function getLength($str)
{
if (''===$str) return 0;
preg_match_all('!(.)\\1*!', $str, $m);
return max(array_map('strlen', $m[0]));
}
这篇关于如何获取字符串中相同字符的最长序列的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!