sscanf()
sscanf() — Parses input from a string according to a format.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
The function sscanf() is the input analog of printf(). sscanf() reads from the string string and interprets it according to the specified format.
Signature
sscanf(string $string, string $format, mixed &...$vars): array|int|null
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $string | string | The input string being parsed. |
| $format | string | The interpreted format for string, 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. * instead of argnum$ suppresses the assignment of this conversion ... |
| &...$vars | mixed | Optionally pass in variables by reference that will contain the parsed values. |
Return value
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 string, null will be returned. When optional parameters are used and the end of the input string is reached before any value has been parsed, -1 is returned.
Example
A runnable snippet:
// 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";
See also
printf(), sprintf(), fprintf(), vprintf(), vsprintf(), vfprintf(), fscanf(), number_format(), date()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page sscanf() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.