Dựa trên điểm tuyệt vời của Georg, tôi sẽ sử dụng Regex. Điều này sẽ xử lý các bản sao phân chia như ddaaddd
với các khóa mảng dd=>2
và ddd=>3
nhưng sẽ chỉ hiển thị một mục cho dd
khi được cung cấp ddaadd
. Để đại diện cho cả hai sẽ yêu cầu một mảng phức tạp hơn:
$string = "ddaabbbccddda";
preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[0], array_map['strlen', $matches[0]]];
arsort[$result];
Nếu bạn chỉ cần số lượng tất cả các lần xuất hiện, hãy thử:
$result = array_count_values[str_split[$string]];
arsort[$result];
Câu trả lời di sản:
Nếu bạn không chia đôi bản sao:
$string = 'aabbbccddd';
$letters = str_split[$string];
$result = array_fill_keys[$letters, 1];
$previous = '';
foreach[$letters as $letter] {
if[$letter == $previous] {
$result[$letter]++;
}
$previous = $letter;
}
arsort[$result];
print_r[$result];
Hoặc cho một cách tiếp cận regex:
preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
[Php 4, Php 5, Php 7, Php 8]
SPERTR_COUNT - Đếm số lượng lần xuất hiện — Count the number of substring occurrences
Sự mô tả
Subr_Count [& nbsp; & nbsp; & nbsp; & nbsp; chuỗi ________ 9, & nbsp; & nbsp; & nbsp; ]: int[
string $haystack
,
string
0,$result = array_count_values[str_split[$string]];
arsort[$result];
int
1 = 0,$result = array_count_values[str_split[$string]];
arsort[$result];
?int
2 = $result = array_count_values[str_split[$string]];
arsort[$result];
3$result = array_count_values[str_split[$string]];
arsort[$result];
]: int
Ghi chú::
Chức năng này không tính các nền tảng chồng chéo. Xem ví dụ dưới đây!
Thông số
$result = array_count_values[str_split[$string]];
arsort[$result];
4Chuỗi để tìm kiếm trong
$result = array_count_values[str_split[$string]];
arsort[$result];
5Bộ nền để tìm kiếm
$result = array_count_values[str_split[$string]];
arsort[$result];
6Phần bù nơi để bắt đầu đếm. Nếu phần bù là âm, đếm bắt đầu từ cuối chuỗi.
$result = array_count_values[str_split[$string]];
arsort[$result];
7Độ dài tối đa sau khi bù được chỉ định để tìm kiếm chuỗi con. Nó đưa ra một cảnh báo nếu phần bù cộng với chiều dài lớn hơn chiều dài
$result = array_count_values[str_split[$string]];
arsort[$result];
4. Một chiều dài âm tính từ cuối $result = array_count_values[str_split[$string]];
arsort[$result];
4.Trả về giá trị
Hàm này trả về một int.int.
Thay đổi
8.0.0 | Subr_Count [& nbsp; & nbsp; & nbsp; & nbsp; chuỗi ________ 9, & nbsp; & nbsp; & nbsp; ]: int |
7.1.0 | Ghi chú: |
Chức năng này không tính các nền tảng chồng chéo. Xem ví dụ dưới đây!
Thông sốsubstr_count[] example
$string = 'aabbbccddd';
$letters = str_split[$string];
$result = array_fill_keys[$letters, 1];
$previous = '';
foreach[$letters as $letter] {
if[$letter == $previous] {
$result[$letter]++;
}
$previous = $letter;
}
arsort[$result];
print_r[$result];
5$string = 'aabbbccddd';
$letters = str_split[$string];
$result = array_fill_keys[$letters, 1];
$previous = '';
foreach[$letters as $letter] {
if[$letter == $previous] {
$result[$letter]++;
}
$previous = $letter;
}
arsort[$result];
print_r[$result];
6$string = 'aabbbccddd';
$letters = str_split[$string];
$result = array_fill_keys[$letters, 1];
$previous = '';
foreach[$letters as $letter] {
if[$letter == $previous] {
$result[$letter]++;
}
$previous = $letter;
}
arsort[$result];
print_r[$result];
7 $result = array_count_values[str_split[$string]];
arsort[$result];
4
$result = array_count_values[str_split[$string]];
arsort[$result];
- Chuỗi để tìm kiếm trong
-
5$result = array_count_values[str_split[$string]]; arsort[$result];
- Bộ nền để tìm kiếm
-
6$result = array_count_values[str_split[$string]]; arsort[$result];
Phần bù nơi để bắt đầu đếm. Nếu phần bù là âm, đếm bắt đầu từ cuối chuỗi. ¶
7$result = array_count_values[str_split[$string]];
arsort[$result];
$string = 'aabbbccddd';
$letters = str_split[$string];
$result = array_fill_keys[$letters, 1];
$previous = '';
foreach[$letters as $letter] {
if[$letter == $previous] {
$result[$letter]++;
}
$previous = $letter;
}
arsort[$result];
print_r[$result];
8$string = 'aabbbccddd';
$letters = str_split[$string];
$result = array_fill_keys[$letters, 1];
$previous = '';
foreach[$letters as $letter] {
if[$letter == $previous] {
$result[$letter]++;
}
$previous = $letter;
}
arsort[$result];
print_r[$result];
9preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
0preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
1 Độ dài tối đa sau khi bù được chỉ định để tìm kiếm chuỗi con. Nó đưa ra một cảnh báo nếu phần bù cộng với chiều dài lớn hơn chiều dài
4. Một chiều dài âm tính từ cuối $result = array_count_values[str_split[$string]];
arsort[$result];
4. ¶$result = array_count_values[str_split[$string]];
arsort[$result];
Trả về giá trị
preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
2preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
3preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
1Hàm này trả về một int. ¶
Thay đổi
preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
5preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
6preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
7preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
8preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
9ddaaddd
0
ddaaddd
1
preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
1Phiên bản ¶
7 bây giờ là vô hiệu.$result = array_count_values[str_split[$string]];
arsort[$result];
ddaaddd
3
ddaaddd
4
ddaaddd
5
preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
8ddaaddd
7
preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
1 Hỗ trợ cho các
6 và $result = array_count_values[str_split[$string]];
arsort[$result];
7 đã được thêm vào. $result = array_count_values[str_split[$string]];
arsort[$result];
7 cũng có thể là $result = array_count_values[str_split[$string]];
arsort[$result];
4 bây giờ. ¶$string = 'aabbbccddd';
$letters = str_split[$string];
$result = array_fill_keys[$letters, 1];
$previous = '';
foreach[$letters as $letter] {
if[$letter == $previous] {
$result[$letter]++;
}
$previous = $letter;
}
arsort[$result];
print_r[$result];
Ví dụ
ddaaddd
9
dd=>2
0
dd=>2
1
dd=>2
2
dd=>2
3
dd=>2
4
dd=>2
5
preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
1Ví dụ #1 ví dụ ¶
Xem thêm
dd=>2
7
Count_chars [] - Trả về thông tin về các ký tự được sử dụng trong chuỗi ¶
strpos [] - Tìm vị trí của lần xuất hiện đầu tiên của chuỗi con trong chuỗi
dd=>2
8
dd=>2
9
ddd=>3
0
ddd=>3
1
ddd=>3
2
ddd=>3
3
ddd=>3
4
ddd=>3
5
preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
1subtr [] - trả về phần của chuỗi ¶
strstr [] - Tìm lần xuất hiện đầu tiên của chuỗi
ddd=>3
7
ddd=>3
8
ddd=>3
9
dd
0tuxedobob ¶ ¶
7 bây giờ là vô hiệu.$result = array_count_values[str_split[$string]];
arsort[$result];
dd
1
dd
2
preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
1