Share on Google+Share on Google+

PHP fscanf() function and example

Advertisement
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

Advertisements

Posted on: October 29, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.