(PHP 4 >= 4.0.1, PHP 5)
sscanf — 根據指定格式解析輸入的字符
說明
mixed sscanf ( string$str
, string $format
[, mixed &$...
] )
這個函數 sscanf() 輸入類似
printf()。 sscanf()
讀取字符串str
然后根據指定格式format
解析, 格式的描述文檔見 sprintf()。
指定的格式字符串中的任意空白匹配輸入字符串的任意空白.也就是說即使是格式字符串中的一個制表符 t 也能匹配輸入 字符串中的一個單一空格字符
參數
str
將要被解析的 字符串.
format
The interpreted format for 解析str
的格式, 除了以下不同外,其余的見
sprintf()的描述文檔:
函數不區分語言地區
F, g, G 和
b 不被支持.
D 表示十進制數字.
i stands for integer with base detection.
n stands for number of characters processed so far.
...
可以選參數將以引用方式傳入,它們的值將被設置為解析匹配的值
返回值
如果僅傳入了兩個參數給這個函數,解析后將返回一個數組,否則,如果可選參數被傳入,這個函數將返回被設置了值的個數
如果format
存在的子字符串比
str
內可用的多,
-1 將被返回.
范例
Example #1 sscanf() 例子
<?php
// getting the serial number
list($serial) = sscanf("SN/2350001", "SN/%d");
// and the date of manufacturing
$mandate = "January 01 2000";
list($month, $day, $year) = sscanf($mandate, "%s %d %d");
echo "Item $serial was manufactured on: $year-" . substr($month, 0, 3) . "-$dayn";
?>
If optional parameters are passed, the function will return the number of assigned values.
Example #2 sscanf() - using optional parameters
<?php
// get author info and generate DocBook entry
$auth = "24tLewis Carroll";
$n = sscanf($auth, "%dt%s %s", $id, $first, $last);
echo "<author id='$id'>
<firstname>$first</firstname>
<surname>$last</surname>
</author>n";
?>
參見
fscanf() - 從文件中格式化輸入 printf() - 輸出格式化字符串 sprintf() - Return a formatted string