sscanf
(PHP 4 >= 4.0.1, PHP 5)
sscanf -- フォーマット文字列に基づき入力を処理する
説明
mixed
sscanf ( string str, string format [, string var1 [, mixed &...]] )
関数 sscanf() は、printf()
の入力版です。sscanf()は、文字列
strを読み込み、これを指定したフォーマット
formatに基づき解釈します。
このフォーマットは、sprintf()のマニュアルに記述されています。
この関数のパラメータが二つだけの場合、処理された値は配列として返されます。
Otherwise, if optional parameters are passed,
the function will return the number of assigned values. The optional
parameters must be passed by reference.
Any whitespace in the format string matches any whitespace in the input
string. This means that even a tab \t in the format string can match a
single space character in the input string.
例 1. sscanf() の例
<?php // シリアル番号を得る $serial = sscanf("SN/2350001", "SN/%d"); // 続いて製造日を得る $mandate = "January 01 2000"; list($month, $day, $year) = sscanf($mandate, "%s %d %d"); echo "Item $serial was manufactured on: $year-" . substr($month, 0, 3) . "-$day\n"; ?>
|
|
オプションのパラメータが指定された場合、この関数は、代入された値
の数を返します。
例 2. sscanf() - オプションパラメータの使用法
<?php // author 情報を取得し、DocBook エントリを生成 $auth = "24\tLewis Carroll"; $n = sscanf($auth, "%d\t%s %s", &$id, &$first, &$last); echo "<author id='$id'> <firstname>$first</firstname> <surname>$last</surname> </author>\n"; ?>
|
|
fscanf(), printf(),
sprintf() も参照ください。