Hướng dẫn how do i count the number of repeated characters in a string in php? - làm cách nào để đếm số ký tự lặp lại trong một chuỗi trong php?

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=>2ddd=>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ệnCount the number of substring occurrences

Sự mô tả

Subr_Count [& nbsp; & nbsp; & nbsp; & nbsp; chuỗi ________ 9, & nbsp; & nbsp; & nbsp; ]: int[
    string $haystack,
    string

$result = array_count_values[str_split[$string]];
arsort[$result];
0,
    int
$result = array_count_values[str_split[$string]];
arsort[$result];
1 = 0
,
    ?int
$result = array_count_values[str_split[$string]];
arsort[$result];
2 =
$result = array_count_values[str_split[$string]];
arsort[$result];
3

]: 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];
4

Chuỗi để tìm kiếm trong

$result = array_count_values[str_split[$string]];
arsort[$result];
5

Bộ nền để tìm kiếm

$result = array_count_values[str_split[$string]];
arsort[$result];
6

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.

$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

Phiên bảnSự mô tả
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

  • Chuỗi để tìm kiếm trong
  • $result = array_count_values[str_split[$string]];
    arsort[$result];
    
    5
  • Bộ nền để tìm kiếm
  • $result = array_count_values[str_split[$string]];
    arsort[$result];
    
    6

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.

$result = array_count_values[str_split[$string]];
arsort[$result];
7

$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];
9

preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
0

preg_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

$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ị

preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
2

preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
3

preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
1

Hà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];
5

preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
6

preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
7

preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
8

preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
9

ddaaddd0

ddaaddd1

preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
1

Phiên bản

$result = array_count_values[str_split[$string]];
arsort[$result];
7 bây giờ là vô hiệu.

ddaaddd3

ddaaddd4

ddaaddd5

preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
8

ddaaddd7

preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
1

Hỗ trợ cho các

$result = array_count_values[str_split[$string]];
arsort[$result];
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à
$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];
4 bây giờ.

Ví dụ

ddaaddd9

dd=>20

dd=>21

dd=>22

dd=>23

dd=>24

dd=>25

preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
1

Ví dụ #1 ví dụ

Xem thêm

dd=>27

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=>28

dd=>29

ddd=>30

ddd=>31

ddd=>32

ddd=>33

ddd=>34

ddd=>35

preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
1

subtr [] - 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=>37

ddd=>38

ddd=>39

dd0

tuxedobob ¶

$result = array_count_values[str_split[$string]];
arsort[$result];
7 bây giờ là vô hiệu.

dd1

dd2

preg_match_all['/[.]\1+/', $string, $matches];
$result = array_combine[$matches[1], array_map['strlen', $matches[0]]];
arsort[$result];
1

Làm cách nào để đếm số lượng ký tự được lặp lại trong một chuỗi?

Approach:..
Tìm sự xuất hiện của ký tự 'A' trong chuỗi đã cho ..
Tìm số lần lặp lại được yêu cầu để tìm 'A' xảy ra ..
Nhân các chuỗi xuất hiện đơn với số ....
Nếu N cho n không phải là bội số của kích thước chuỗi đã cho thì chúng ta sẽ tìm thấy 'A' xảy ra trong phần phụ còn lại ..

Làm thế nào để bạn tìm thấy một nhân vật lặp lại?

Để tìm ký tự trùng lặp từ chuỗi, chúng tôi đếm sự xuất hiện của từng ký tự trong chuỗi.Nếu số lượng lớn hơn 1, nó ngụ ý rằng một ký tự có mục nhập trùng lặp trong chuỗi.Trong ví dụ trên, các ký tự được tô sáng trong màu xanh lá cây là các ký tự trùng lặp.count the occurrence of each character in the string. If count is greater than 1, it implies that a character has a duplicate entry in the string. In above example, the characters highlighted in green are duplicate characters.

Làm cách nào tôi có thể in các ký tự trùng lặp trong một chuỗi trong PHP?

Php $ chuỗi = "aAbbBCCDDD";$ mảng = mảng [$ mảng];foreach [Count_chars [$ chuỗi, 1] là $ i => $ val] {$ Count = chr [$ i];$ mảng [] = $ val.",".$ đếm;} print_r [$ mảng];?>print_r[$array]; ?>

Bài Viết Liên Quan

Chủ Đề