PHP의 substr_count () 함수
예
"세계"의 수를 계산하는 문자열에 나타납니다 :
<?php
echo substr_count("Hello world. The world is nice","world");
?>
echo substr_count("Hello world. The world is nice","world");
?>
»인스턴스를 실행
substr_count () 함수는 캐릭터 스트링의 발생 횟수를 산출한다.
참고 : 문자열은 대소 문자를 구분합니다.
참고 :이 기능은 중복 문자열을 계산하지 않습니다 (예 2 참조).
참고 : 인수를 시작 더한 길이 매개 변수 문자열의 길이보다 큰 경우에 경고를 생성 함수 (실시 예 3 참조).
문법
substr_count( string,substring,start,length )
参数 | 描述 |
---|---|
string | 必需。规定要检查的字符串。 |
substring | 必需。规定要检索的字符串。 |
start | 可选。规定在字符串中何处开始搜索。 |
length | 可选。规定搜索的长度。 |
기술 세부 사항
반환 값 : | 문자열 문자열의 발생 수를 돌려줍니다. |
---|---|
PHP 버전 : | 4+ |
업데이트 로그 : | PHP 5.1에서는 시작과 길이 매개 변수를 추가합니다. |
더 많은 예제
예 1
모든 매개 변수를 사용하여
<?php
$str = "This is nice";
echo strlen($str)."<br>"; // Using strlen() to return the string length
echo substr_count($str,"is")."<br>"; // The number of times "is" occurs in the string
echo substr_count($str,"is",2)."<br>"; // The string is now reduced to "is is PHP"
echo substr_count($str,"is",3)."<br>"; // The string is now reduced to "s is PHP"
echo substr_count($str,"is",3,3)."<br>"; // The string is now reduced to "s i"
?>
$str = "This is nice";
echo strlen($str)."<br>"; // Using strlen() to return the string length
echo substr_count($str,"is")."<br>"; // The number of times "is" occurs in the string
echo substr_count($str,"is",2)."<br>"; // The string is now reduced to "is is PHP"
echo substr_count($str,"is",3)."<br>"; // The string is now reduced to "s is PHP"
echo substr_count($str,"is",3,3)."<br>"; // The string is now reduced to "s i"
?>
»인스턴스를 실행
예 2
중복 문자열 :
<?php
$str = "abcabcab";
echo substr_count($str,"abcab"); // This function does not count overlapped substrings
?>
$str = "abcabcab";
echo substr_count($str,"abcab"); // This function does not count overlapped substrings
?>
»인스턴스를 실행
예 3
문자열의 길이에 걸쳐 시작과 길이 매개 변수 경우, 함수는 경고를 출력합니다
<?php
echo $str = "This is nice";
substr_count($str,"is",3,9);
?>
echo $str = "This is nice";
substr_count($str,"is",3,9);
?>
때문에 값의 길이 스트링 (3 + 9 이상 12)의 길이를 초과한다. 그래서 출력은 경고 것입니다.
PHP 문자열 참조