超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<sprintfstr_ireplace>
Last updated: Fri, 22 Jun 2007

sscanf

(PHP 4 >= 4.0.1, PHP 5)

sscanf --  Parses input from a string according to a format

Description

mixed sscanf ( string str, string format [, mixed &...])

The function sscanf() is the input analog of printf(). sscanf() reads from the string str and interprets it according to the specified format. If only two parameters were passed to this function, the values parsed will be returned as an array. 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() Example

<?php
// getting the serial number
$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) . "-$day\n";
?>
If optional parameters are passed, the function will return the number of assigned values.

例子 2. sscanf() - using optional parameters

<?php
// get author info and generate DocBook entry
$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"
;
?>

See also fscanf(), printf(), and sprintf().




add a note add a note User Contributed Notes
sscanf
clcollie at mindspring dot com
12-Sep-2000 07:59
Actually sscanf()_always_ returns an array if you specify less return variables than format specifiers. i may change this to return a scalar if only a single format specifier exists.
 Note that sscanf() is (almost) the complete functional equivalent of its "C" counterpart, so you can do the following to get the expected effect :

  sscanf("SN/2350001","SN/%d",&$serial)

The array return was a nicety for PHP.

<sprintfstr_ireplace>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22