我想计算单个字符以字符串但连续的方式出现。
<?php
echo $strin = "abcaaabbcccccdaasvvbbvvtteesgggg";
echo "</br>";
$str_l = strlen($strin);
$i = $j = 0;
$temp_c = "";
for($i;$i<$str_l;$i++){
$j = 0;
$j = substr_count($strin, $strin[$i]);
echo $strin[$i]." is repeated ".$j." times </br>";
}
?>
答案 0 :(得分:2)
您可以执行此操作-
$string = "abcaaabbcccccdaasvvbbvvtteesgggg";
// Split string by character and convert to array
$temp = str_split($string);
// Count values present in array
array_count_values($temp);
输出
Array ( [a] => 6 [b] => 5 [c] => 6 [d] => 1 [s] => 2 [v] => 4 [t] => 2 [e] => 2 [g] => 4 )
JavaScript scrollTo method does nothing?
连续出现
// split and create array with unique characters
$new = array_unique(str_split($string));
$chars = [];
foreach($new as $c) {
// regex check for character's multiple occurrences
preg_match_all("/($c)\\1+/", $string, $matches);
// Store the count
$chars[$c] = count($matches[0]);
}
print_r($chars);
Outpur
Array
(
[a] => 2
[b] => 2
[c] => 1
[d] => 0
[s] => 0
[v] => 2
[t] => 1
[e] => 1
[g] => 1
)