计算字符串中连续出现的特定相同字符数-PHP

计算字符串中连续出现的特定相同字符数-PHP,php,sequence,strlen,Php,Sequence,Strlen,我试图计算一些“连胜”,特别是连续赢和输的最高次数,但也要计算大多数没有赢的比赛和没有输的比赛 我有一根像这样的线;'wwwddwwlllllll' 为此,我需要能够返回: W charector的最长连续运行(然后我将复制L) 没有W charector的最长连续运行(然后我将复制L) 我已经找到并调整了以下内容,这些内容将遍历我的数组并告诉我最长的序列,但我似乎无法调整它以满足上述标准 非常感谢所有的帮助和学习:) 函数getLongestSequence($sequence){ $sl

我试图计算一些“连胜”,特别是连续赢和输的最高次数,但也要计算大多数没有赢的比赛和没有输的比赛

我有一根像这样的线;'wwwddwwlllllll'

为此,我需要能够返回:

  • W charector的最长连续运行(然后我将复制L)
  • 没有W charector的最长连续运行(然后我将复制L)
我已经找到并调整了以下内容,这些内容将遍历我的数组并告诉我最长的序列,但我似乎无法调整它以满足上述标准

非常感谢所有的帮助和学习:)

函数getLongestSequence($sequence){ $sl=strlen($sequence); $longest=0; 对于($i=0;$i<$sl;) { $substr=substr($sequence,$i); $len=strspn($substr,$substr{0});if($len>$longest) $longest=$len; $i+=$len; } 返回$longest; } echo getLongestSequence($sequence);
您可以使用正则表达式检测相同字符的序列:

$string = 'WWWDDWWWLLWLLLL';
// The regex matches any character -> . in a capture group ()
// plus as much identical characters as possible following it -> \1+
$pattern = '/(.)\1+/';

preg_match_all($pattern, $string, $m);
// sort by their length
usort($m[0], function($a, $b) {
    return (strlen($a) < strlen($b)) ? 1 : -1;
});

echo "Longest sequence: " . $m[0][0] . PHP_EOL;
$string='wwwddwwlllllll';
//正则表达式匹配任何字符->。在捕获组()中
//在其后面加上尽可能多的相同字符->\1+
$pattern='/()\1+/';
preg_match_all($pattern,$string,$m);
//按长度排序
usort($m[0],函数($a,$b){
报税表(strlen($a)
您可以使用正则表达式检测相同字符的序列:

$string = 'WWWDDWWWLLWLLLL';
// The regex matches any character -> . in a capture group ()
// plus as much identical characters as possible following it -> \1+
$pattern = '/(.)\1+/';

preg_match_all($pattern, $string, $m);
// sort by their length
usort($m[0], function($a, $b) {
    return (strlen($a) < strlen($b)) ? 1 : -1;
});

echo "Longest sequence: " . $m[0][0] . PHP_EOL;
$string='wwwddwwlllllll';
//正则表达式匹配任何字符->。在捕获组()中
//在其后面加上尽可能多的相同字符->\1+
$pattern='/()\1+/';
preg_match_all($pattern,$string,$m);
//按长度排序
usort($m[0],函数($a,$b){
报税表(strlen($a)
您可以使用以下代码实现特定字符串中连续字符的最大计数

       $string = "WWWDDWWWLLWLLLL";
        function getLongestSequence($str,$c) {
        $len = strlen($str);
        $maximum=0;
        $count=0;
        for($i=0;$i<$len;$i++){
            if(substr($str,$i,1)==$c){
                $count++;
                if($count>$maximum) $maximum=$count;
            }else $count=0;
        }
        return $maximum;
        }
        $match="W";//change to L for lost count D for draw count
        echo getLongestSequence($string,$match);
$string=“wwwdwwlll”;
函数getLongestSequence($str,$c){
$len=strlen($str);
$max=0;
$count=0;
对于($i=0;$i$maximum)$maximum=$count;
}否则$count=0;
}
返回$max;
}
$match=“W”//更改为L表示丢失计数D表示抽签计数
echo getLongestSequence($string,$match);

您可以使用以下代码实现特定字符串中连续字符的最大计数

       $string = "WWWDDWWWLLWLLLL";
        function getLongestSequence($str,$c) {
        $len = strlen($str);
        $maximum=0;
        $count=0;
        for($i=0;$i<$len;$i++){
            if(substr($str,$i,1)==$c){
                $count++;
                if($count>$maximum) $maximum=$count;
            }else $count=0;
        }
        return $maximum;
        }
        $match="W";//change to L for lost count D for draw count
        echo getLongestSequence($string,$match);
$string=“wwwdwwlll”;
函数getLongestSequence($str,$c){
$len=strlen($str);
$max=0;
$count=0;
对于($i=0;$i$maximum)$maximum=$count;
}否则$count=0;
}
返回$max;
}
$match=“W”//更改为L表示丢失计数D表示抽签计数
echo getLongestSequence($string,$match);

Try正则表达式可能重复,具体来说,您正在查找
W+
[^W]+
。感谢您的回复@ValentinRodygin,我已经看到了这一点,但是我看不到如何引用特定的charector,或者查找除特定charector之外的任何字符串。georg reg ex也许可以加入其中?可能是Try正则表达式的副本,具体来说,您正在寻找
W+
[^W]+
。感谢您的回复@ValentinRodygin,我已经看到了这一点,但是我看不到如何引用特定的charector,或者查找除特定charector之外的任何字符串。georg也许reg-ex可以加入其中?谢谢-它可以工作,但我没有提到我试图实现的网站目前在PHP5.2上失败,对此有什么建议吗?哦,我明白了。PHP5.2不支持匿名函数(如上面示例中的
usort
所使用的),我建议在该服务器上更新PHP。然而,这里有一个适用于所有版本php的示例:感谢您的帮助,我现在收到另一个警告:warning:usort()[function.usort]:无效的比较函数然后显示最长的序列为WWW。如果
$a
$b
具有相同的长度,则该函数可能会显式返回
0
。你能试试吗?谢谢,我找到了一种输出特定字符最长序列长度的方法。但我可以使用preg_match_all来查找字符串中找不到特定字符的最长部分吗?例如,如果特定的charector是W,我想在没有charector的地方找到最长数量的charector…谢谢-它可以工作,但我没有提到我尝试实现的站点目前在PHP5.2上失败,对此有什么建议吗?哦,我明白了。PHP5.2不支持匿名函数(如上面示例中的
usort
所使用的),我建议在该服务器上更新PHP。然而,这里有一个适用于所有版本php的示例:感谢您的帮助,我现在收到另一个警告:warning:usort()[function.usort]:无效的比较函数然后显示最长的序列为WWW。如果
$a
$b
具有相同的长度,则该函数可能会显式返回
0
。你能试试吗?谢谢,我找到了一种输出特定字符最长序列长度的方法。但我可以使用preg_match_all来查找字符串中找不到特定字符的最长部分吗?例如,如果特定的字符是W,我想在不存在字符的地方找到最长的字符数。。。