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 "";
}