PHP fscanf() function and example


 

PHP fscanf() function and example

In this segment of the PHP tutorial we will learn the use of PHP fscanf() file handling function and its example.

In this segment of the PHP tutorial we will learn the use of PHP fscanf() file handling function and its example.

Syntax

mixed fscanf file_handle,format_specifier [, mixed &$... ] )

  • fscanf() function reads the contents of the file according to the given formatspecifier
  • Format specifier can be like the C language format specifier
  • fscanf() uses %d, %c, %s etc format specifier.
  • By default it returns mixed type.
  • But if two parameter is used then it returns array

Example of PHP fscant() Function PHP
Code for PHP fsscant() Unction PHP

<?php
    $file
=fopen("c:/rose1/ram.txt","r");

    while
($ar1=fscanf($file,"%s\t%s"))
     {
      for
($x=0;$x<sizeof($ar1);$x++)
        {
        echo
$ar1[$x];
        if
($x%2!=0)
        echo
"<br>";
        }
     }
?>
Output
first_namelast_name
ramkumar
shyamsinha

Ads