>  기사  >  백엔드 개발  >  문자열의 모든 정보를 반환하는 PHP 함수 count_chars()

문자열의 모든 정보를 반환하는 PHP 함수 count_chars()

黄舟
黄舟원래의
2017-11-02 10:20:101633검색

"Hello World!"(모드 3)에 사용된 다양한 문자를 모두 포함하는 string을 반환합니다.

<?php
$str = "Hello World!";
echo count_chars($str,3);
?>

정의 및 사용법

count_chars() Function문자열에 사용된 문자 수를 반환합니다. (예를 들어, ASCII 문자가 문자열에 나타나는 횟수 또는 문자가 문자열에 이미 사용되었는지 여부)

구문

count_chars(string,mode)

매개변수 ~     구문 확인할 문자열을 지정합니다. ​ ​

모드 ​ ​ ​ ​ ​ ​ 선택사항입니다. 반환 모드를 지정합니다. 기본값은 0입니다. 다음과 같은 다양한 반품 모드가 있습니다.

                                                                                        배열 s가 나열됩니다. 횟수 값이 0 -2 -array, ASCII 값보다 큽니다. 는 키 이름이고, 횟수가 키 값이고, 횟수의 값만 0입니다.

3 -문자열, 모두 다른 문자 사용

.带 4 -사용되지 않은 모든 다른 문자가 포함된 문자열

기술 세부 정보

반환 값: 지정된 모드 매개변수에 따라 다릅니다.

PHP 버전: 4+

추가 예제

예제 1

"Hello World!"에서 사용되지 않은 모든 문자가 포함된 문자열 반환(모드 4):

<?php
$str = "Hello World!";
echo count_chars($str,4);
?>

예제 2

在本实例中,我们将使用 count_chars() 来检查字符串,返回模式设置为 1。模式 1 将返回一个数组,ASCII 值为键名,出现的次数为键值:

<?php
$str = "Hello World!";
print_r(count_chars($str,1));
?>

实例 3

统计 ASCII 字符在字符串中出现的次数另一个实例:

<?php
$str = "PHP is pretty fun!!";
$strArray = count_chars($str,1);

foreach ($strArray as $key=>$value)
{
echo "The character <b>&#39;".chr($key)."&#39;</b> was found $value time(s)<br>";
}
?>

count_chars实例

<?php
$data  =  "Two Ts and one F." ;
foreach ( count_chars ( $data ,  1 ) as  $i  =>  $val ) {   
echo  "There were  $val  instance(s) of \""  ,  chr ( $i ) ,  "\" in the string.<br/>" ;
}
?>

运行结果:

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.

위 내용은 문자열의 모든 정보를 반환하는 PHP 함수 count_chars()의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.