sscanf
(PHP 4 >= 4.0.1, PHP 5, PHP 7)
sscanf — Parses input from a string according to a format
Description
The function sscanf() is the input analog of
printf(). sscanf() reads
from the string str
and interprets it
according to the specified format
, which is
described in the documentation for sprintf().
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.
Parameters
-
str
-
The input string being parsed.
-
format
-
The interpreted format for
str
, which is described in the documentation for sprintf() with following differences:- Function is not locale-aware.
- F, g, G and b are not supported.
- D stands for decimal number.
- i stands for integer with base detection.
- n stands for number of characters processed so far.
- s stops reading at any whitespace character.
-
...
-
Optionally pass in variables by reference that will contain the parsed values.
Return Values
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.
If there are more substrings expected in the format
than there are available within str
,
-1 will be returned.
Examples
Example #1 sscanf() Example
<?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) . "-$day\n";
?>
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 = "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
- printf() - Output a formatted string
- sprintf() - Return a formatted string
- fprintf() - Write a formatted string to a stream
- vprintf() - Output a formatted string
- vsprintf() - Return a formatted string
- vfprintf() - Write a formatted string to a stream
- fscanf() - Parses input from a file according to a format
- number_format() - Format a number with grouped thousands
- date() - Format a local time/date
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-function.sscanf.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.