• PHP Video Tutorials

PHP - Function fscanf()



The fscanf() function can parse input from an open file according to a specified format.

Syntax

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

This function is similar to the sscanf() function. But it can take input from a file associated with handle and interpret input according to a specified format.

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

Example

<?php
   $handle = fopen("/PhpProject/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

Ravi
Lead
AUS
php_function_reference.htm
Advertisements