downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

strstr> <strrpos
Last updated: Fri, 13 Nov 2009

view this page in

strspn

(PHP 4, PHP 5)

strspn 指定したマスク内に含まれる文字からなる文字列の最初のセグメントの長さを探す

説明

int strspn ( string $subject , string $mask [, int $start [, int $length ]] )

subject の中で、 mask 内の文字が連続する最初の部分の長さを返します。

startlength を省略した場合は、subject 全体を調べます。 指定した場合は、strspn(substr($subject, $start, $length), $mask) (詳細は substr を参照ください) をコールするのと同じ働きをします。

たとえば、

<?php
$var 
strspn("42 is the answer to the 128th question.""1234567890");
?>

このコードは、$var2 を代入します。これは、 subject の中で "1234567890" 内の文字だけで構成される 最初の部分が "42" であるためです。

パラメータ

subject

調べたい文字列。

mask

セグメントを数える際に許可する文字の一覧。

start

subject の中で調べ始める位置。

start に非負の値を指定すると、 strspn()subjectstart 番目の位置から調査を始めます。 たとえば、文字列 'abcdef' において 0 番目の位置にある文字は 'a' で 2 番目の位置にある文字は 'c' のようになります。

start に負の値を指定すると、 strspn()subject の最後から start 番目の位置から調査を始めます。

length

subject 内で調べる部分の長さ。

length に非負の値を指定すると、 subject の開始位置から length 文字ぶんの範囲を調査します。

length に負の値を指定すると、 subject の調査範囲は 開始位置から始まって subject の最後から length だけさかのぼったところまでとなります。

返り値

str1 の中で、全て str2 の中の文字からなる最初のセグメントの長さを返します。

変更履歴

バージョン 説明
4.3.0 start および length が追加されました。

例1 strspn() の例

<?php
echo strspn("foo""o"12); // 出力は 2
?>

注意

注意: この関数はバイナリデータに対応しています。

参考

  • strcspn() - マスクにマッチしない最初のセグメントの長さを返す



strstr> <strrpos
Last updated: Fri, 13 Nov 2009
 
add a note add a note User Contributed Notes
strspn
Dmitry Mazur
04-Mar-2009 06:23
The second parameter is a set of allowed characters.
strspn will return an zero-based index of a first non-allowed character.
barry dot balkowski at gmail dot com
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
This function is significantly faster for checking illegal characters than the equivalent preg_match() method.

strstr> <strrpos
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites