Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php查找发生次数(次数)_Php_Numbers - Fatal编程技术网

php查找发生次数(次数)

php查找发生次数(次数),php,numbers,Php,Numbers,有人能帮忙吗 我需要以下函数来执行此操作 $x = 'AAAABAA'; $x2 = 'ABBBAA'; function foo($str){ //do something here.... return $str; } $x3 = foo($x); //output: A4B1A2 $x4 = foo($x2);//output: A1B3A2 他是你的朋友吗 或者更确切地说是这个 function foo($str) { $out = ''; preg_m

有人能帮忙吗

我需要以下函数来执行此操作

$x = 'AAAABAA';
$x2 = 'ABBBAA';

function foo($str){
   //do something here....
   return $str;
}

$x3 = foo($x); //output: A4B1A2
$x4 = foo($x2);//output: A1B3A2
他是你的朋友吗

或者更确切地说是这个

function foo($str) {
    $out = '';
    preg_match_all('~(.)\1*~', $str, $m, PREG_SET_ORDER);
    foreach($m as $p) $out .= $p[1] . strlen($p[0]);
    return $out;
}

不是因为它会返回$x=A6B2,这与$x='A4B1A2'不同;那会有什么不同呢?
function foo($str) {
    $s = str_split($str);
    if (sizeof($s) > 0) {
        $current = $s[0];
        $output = $current;
        $count = 0;
        foreach ($s as $char) {
            if ($char != $current ) {           
                $output .= $count;
                $current = $char;
                $output .= $current;
                $count = 1;
            }
            else {
                $count++;
            }
        }
        $output .= $count;
        return $output;
    }
    else
        return "";
}