Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Files and I/O

PHP fscanf() and sscanf() functions

Reference for PHP sscanf() and fscanf(): parse strings and file lines with printf-style formats, %d %s %f %c and character sets, return values and pitfalls.

sscanf() is the reverse of sprintf(): it reads values out of a string according to a format. fscanf() does the same for one line read from a file handle. Instead of writing a regular expression, you describe the shape of the text with placeholders like %d (integer) and %s (word).

They suit fixed, simple layouts such as "12:30", "Ada 36" or log lines with predictable fields.

PHP
<?php
[$h, $m] = sscanf("07:45", "%d:%d");
echo $h + 1, " ", $m;   // 8 45

Try it

PHP Parsing strings with sscanf()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Reading lines from a file with fscanf()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
sscanf(string $string, string $format, mixed &...$vars): array|int|null
fscanf(resource $stream, string $format, mixed &...$vars): array|int|false|null
Specifier Matches
%d A signed decimal integer.
%f, %e, %g A floating-point number.
%s A run of non-whitespace characters.
%c One character (or a fixed count, as in %3c).
%x, %X, %o Hexadecimal or octal integers.
%[a-z] A run of characters from the set; %[^,] is everything up to a comma.
%n Stores the number of characters consumed so far.
%% A literal percent sign.

A number before the letter limits the width (%4d); %*d reads a value but discards it.

Name Description
$string / $stream Text to parse, or an open file handle.
$format The format string. Whitespace in it matches any amount of whitespace.
$vars Optional references that receive the values.

Return value: without $vars, an array of parsed values (unmatched ones are null). With $vars, the number of values assigned. When the input is empty, sscanf() returns null without variables (and -1 with variables); fscanf() returns false at end of file.

Errors / Exceptions: a ValueError is thrown for an invalid format, or if the number of variables does not match the format.

Usage notes

  • Check the result. A failed conversion gives null in that slot, so test with === null before using the value. Without variables, sscanf('hello', '%d') returns [null].
  • Numbers are converted. %d returns an int, %f a float, %s a string.
  • %s stops at whitespace. A name with a space needs a character set, e.g. %[^,].
  • Not a validator. The format ignores trailing text, so sscanf('12abc', '%d') still returns [12]. Check the rest of the input if that matters.
  • One line per call: fscanf() reads a single line each time, so use it in a loop that stops when it returns false.
  • Alternatives. Use explode(), preg_match() or str_getcsv() for delimiter-based or complex patterns.
  • Reference parameters must be variables, not expressions or array elements that do not exist yet.