PHP Function Reference

PHP fscanf() Function



The PHP fscanf() function parses input from a file according to the specified format. This function is similar to sscanf(), but it takes its input from a file associated with stream. Each call to this function reads one line from the file.

If only two parameters were passed, the values parsed will be returned as an array. 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.

Syntax

fscanf(stream, format, vars)

Parameters

stream Required. Specify a file system pointer resource that is typically created using fopen().
format

Required. Specify the format string. The possible values of %specifier are:

%specifierDescription
%%Returns a percent sign
%cThe character according to the ASCII value
%dSigned decimal number (negative, zero or positive)
%DSigned decimal number (negative, zero or positive)
%iInteger with base detection
%eScientific notation using a lowercase (e.g. 1.2e+3)
%EScientific notation using a uppercase (e.g. 1.2E+3)
%uUnsigned decimal number (zero or positive)
%fFloating-point number
%oOctal number
%sString (stops reading at any whitespace character)
%xHexadecimal number (lowercase letters)
%XHexadecimal number (uppercase letters)
%nNumber of characters processed so far

Additional format value width can be placed between the % and the specifier, (e.g. %10f). This can be used to specify an integer which tells minimum width held of to the variable value.
vars Optional. Specify variables by reference which will contain the parsed values.

Return Value

If only two parameters were passed, the values parsed will be returned as an array. 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. On other errors, false will be returned.

Example: fscanf() example

Lets assume that we have a file called employees.txt. This file contains following content:

John  25  3000
Marry 24  2750
Jo  27  2800
Kim 30  3100
Ramesh  28  3000

In the example below, the file is opened using fopen() function. Then the content of the file is parsed according to the specified format using fscanf() function. After performing this operation, it is closed using fclose() function.

<?php
//open the file in read mode
$fp = fopen("employees.txt", "r");

//parsing input from a file according to the specified format
while ($employee_info = fscanf($fp, "%s\t%d\t%d\n")) {
  list ($name, $age, $salary) = $employee_info;
  
  echo "$name is $age years old and earns $salary dollars.\n";
}

//closing the file
fclose($fp);
?>

The output of the above code will be:

John is 25 years old and earns 3000 dollars.
Marry is 24 years old and earns 2750 dollars.
Jo is 27 years old and earns 2800 dollars.
Kim is 30 years old and earns 3100 dollars.
Ramesh is 28 years old and earns 3000 dollars.

Example: using optional parameters

The example below shows how to use optional parameters with this function.

<?php
//open the file in read mode
$fp = fopen("employees.txt", "r");

//parsing input from a file according to the specified format
while (fscanf($fp, "%s\t%d\t%d\n", $name, $age, $salary)) {
  echo "$name is $age years old and earns $salary dollars.\n";
}

//closing the file
fclose($fp);
?>

The output of the above code will be:

John is 25 years old and earns 3000 dollars.
Marry is 24 years old and earns 2750 dollars.
Jo is 27 years old and earns 2800 dollars.
Kim is 30 years old and earns 3100 dollars.
Ramesh is 28 years old and earns 3000 dollars.

❮ PHP Filesystem Reference