English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and Examples of PHP fscanf() Function

PHP Filesystem Reference Manual

The fscanf() function parses input from an open file according to the specified format.

Syntax

mixed fscanf ( resource $handle , string $format [, mixed &$... ] )

This function is similar to the sscanf() function. However, it can obtain input from the file associated with handle and interpret the input according to the specified format.

Any whitespace in the string format can match any whitespace in the input stream, which means that even the tab character \t in the string format can match a single space character in the input stream, and fscanf() can read a line from the file each time it is called.

Online Example

<?php
   $handle = fopen("Users.txt", "r");
   while($userinfo = fscanf($handle, "%s\t%s\t%s\n")) {
      list($name, $profession, $countrycode) = $userinfo;
   }
   echo $name . "\n";
   echo $profession . "\n";
   echo $countrycode;
   
   fclose($handle);
?>

Output Result

Ravi
Lead
AUS

PHP Filesystem Reference Manual