The second parameter is a set of allowed characters.
strspn will return an zero-based index of a first non-allowed character.
strspn
(PHP 4, PHP 5)
strspn — 마스크에 매치하는 초기 세그먼트의 길이를 찾는다
설명
int strspn
( string $str1
, string $str2
[, int $start
[, int $length
]] )
마스크에 매치하는 초기 세그먼트의 길이를 찾습니다.
간단한 코드:
<?php
$var = strspn ("42 is the answer, what is the question ...", "1234567890");
?>
문자열 "42"가 "1234567890"으로 구성되는 가장 긴 세그먼트이기에, $var에 2가 들어갑니다.
인수
- str1
-
첫번째 문자열.
- str2
-
두번째 문자열.
- start
-
검사할 문자열의 시작 위치. 음수는 문자열 마지막에서부터 위치를 셉니다.
- length
-
검사할 문자열의 길이. 음수는 문자열 끝에서부터 길이를 설정합니다.
반환값
str1 에서 str2 의 모든 문자들로 구성되는 최초 세그먼트의 길이를 반환합니다.
| 버전 | 설명 |
|---|---|
| 4.3.0 | start 와 length 인수 추가 |
예제
Example #1 strspn() 예제
<?php
echo strspn("foo", "o", 1, 2); // 2
?>
주의
Note: 이 함수는 바이너리 안전입니다.
strspn
Dmitry Mazur
04-Mar-2009 06:23
04-Mar-2009 06:23
barry dot balkowski at gmail dot com
08-Aug-2008 09:12
08-Aug-2008 09:12
It took me some time to understand the way this function works…
I’ve compiled my own explanation with my own words that is more understandable for me personally than the official one or those that can be found in different tutorials on the web.
Perhaps, it will save someone several minutes…
<?php
strspn(string $haystack, string $char_list [, int $start [, int $length]])
?>
The way it works:
- searches for a segment of $haystack that consists entirely from supplied through the second argument chars
- $haystack must start from one of the chars supplied through $char_list, otherwise the function will find nothing
- as soon as the function encounters a char that was not mentioned in $chars it understands that the segment is over and stops (it doesn’t search for the second, third and so on segments)
- finally, it measures the segment’s length and return it (i.e. length)
In other words it finds a span (only the first one) in the string that consists entirely form chars supplied in $chars_list and returns its length
B Crawford
03-Oct-2007 12:20
03-Oct-2007 12:20
This function is significantly faster for checking illegal characters than the equivalent preg_match() method.
