count_chars
(PHP 4 , PHP 5)
count_chars --
문자열 안에 사용한 문자에 대한 정보를 반환합니다.
설명
mixed
count_chars ( string string [, int mode])
string 안의 모든 바이트값(0..255)의 발견 수를 세고
여러 방법으로 반환합니다. 선택적인 인자 mode의
기본값은 0입니다. mode에 따른
count_chars()의 반환값은 다음과 같습니다:
0 - 바이트 값을 키로, 모든 바이트의 빈도를 값으로 가지는 배열.
1 - 0과 같지만, 빈도가 1 이상인 바이트값만 반환합니다.
2 - 0과 같지만, 빈도가 0인 바이트값만 반환합니다.
3 - 사용한 모든 바이트값을 가지는 문자열을 반환합니다.
4 - 사용하지 않은 모든 바이트값을 가지는 문자열을 반환합니다.
예 1. count_chars() 예제
<?php
$data = "Two Ts and one F.";
$result = count_chars($data, 0);
for ($i=0; $i < count($result); $i++) { if ($result[$i] != 0) echo "There were $result[$i] instance(s) of \"" , chr($i) , "\" in the string.\n"; }
?>
|
출력은:
There were 4 instance(s) of " " in the string.
There were 1 instance(s) of "." in the string.
There were 1 instance(s) of "F" in the string.
There were 2 instance(s) of "T" in the string.
There were 1 instance(s) of "a" in the string.
There were 1 instance(s) of "d" in the string.
There were 1 instance(s) of "e" in the string.
There were 2 instance(s) of "n" in the string.
There were 2 instance(s) of "o" in the string.
There were 1 instance(s) of "s" in the string.
There were 1 instance(s) of "w" in the string. |
|
참고: strpos(),
substr_count().