sscanf()
Our article is about the PHP function sscanf(), which is used to parse input from a string. This function is useful for extracting data from strings that have a
The PHP sscanf() function reads a string and pulls out values according to a format you describe — it is the inverse of sprintf(). Where sprintf() builds a formatted string from variables, sscanf() takes apart a formatted string back into variables. It shines when you have text in a predictable shape (dates, coordinates, log lines, ID codes) and want clean, typed pieces without writing a regular expression.
This chapter covers the syntax, the two ways to receive results, the format specifiers you will actually use, and the common pitfalls.
Syntax
sscanf(string $string, string $format, mixed &...$vars): array|int|null$string— the input text to parse.$format— a template describing what to read, using%specifiers (the same familyprintfuses).&...$vars— optional variables, passed by reference, that receive the parsed values.
How sscanf() behaves depends on whether you pass those extra variables:
| Call style | Return value |
|---|---|
Only $string and $format | An array of the parsed values |
With reference variables after $format | An int: how many values were successfully assigned |
Return the values as an array
If you omit the reference arguments, sscanf() hands everything back as an array. This is the cleanest style in modern PHP and avoids passing variables by reference.
%s reads the next non-whitespace word (John), and %d reads an integer (25, stored as a real int, not the string "25"). The output is:
John
25Assign directly into variables
Passing variables after the format assigns into them directly. In this mode the return value is the count of fields that matched, which is handy for validating input.
<?php
$input = 'John 25';
$matched = sscanf($input, '%s %d', $name, $age);
echo $matched . "\n"; // 2 (both fields were read)
echo $name . "\n"; // John
echo $age; // 25
?>Note: in PHP 8 the call-time
&prefix (e.g.sscanf($s, $f, &$name)) was removed. Just pass the plain variable —sscanf()declares those parameters as by-reference itself.
Common format specifiers
| Specifier | Reads |
|---|---|
%s | A string up to the next whitespace |
%d | A signed decimal integer |
%f | A floating-point number |
%x | A hexadecimal integer |
%c | A single character |
%% | A literal % sign |
Literal characters in the format (spaces, slashes, colons) must appear in the input too. This makes sscanf() excellent for fixed-shape data such as dates:
<?php
$date = '2026-06-21';
[$year, $month, $day] = sscanf($date, '%d-%d-%d');
printf("Year=%d Month=%d Day=%d", $year, $month, $day);
// Year=2026 Month=6 Day=21
?>When to use sscanf() vs. alternatives
- Reach for
sscanf()when the format is fixed and simple and you want typed results in one line. - Use
explode()when you only need to split on a delimiter and keep everything as strings. - Use
preg_match()when the structure is irregular or needs validation rules that go beyond simple field types. - To do the reverse — assemble a formatted string — use
sprintf()orprintf().
To read formatted input directly from a file instead of a string, see fscanf(), which works the same way line by line.
Gotchas
%sstops at whitespace. It will not capture a multi-word value. To read the rest of a line, use a scanset like%[^\n].- A non-matching field aborts the rest. If
%dis expected but the input has letters, parsing stops; the count return value lets you detect this. - Unmatched trailing variables become
null. Always check the returned count before trusting later variables.