Jump to: navigation, search

Php fscanf

From w3cyberlearnings

Contents

PHP function fscanf

This function parses input from a file according to a format.

Syntax fscanf

  • file is a file handle
  • format: a format string
fscanf(file, format);

format

  •  % - returns a percent sign.
  • b - is an integer and display it as a binary number.
  • c - is an integer and display it as a ASCII value.
  • d - is an integer and display as a signed decimal number.
  • e - is scientific notation with lowercase letter e(e.g. 1.2e+2).
  • E - is scientific notation with capital letter E(e.g.1.2E+2).
  • u - is an integer, and display as an unsigned decimal number.
  • f- is a float, and display as a floating-point number. (local aware)
  • F - is a float, and display as a floating-point number (non-locale aware).
  • g - is shorter of %e and %f.
  • G - is shorter of %E and %f.
  • o - is an integer, and display as an octal number.
  • s - is string and display as a string.
  • x - is an integer and display as a hexadecimal number (with lowercase letters).
  • X - is an integer and display as a hexadecimal number (with uppercase letters).


Example 1

File (file.txt)

Bob Maat USA
Jing Kun USA
Lee Mark USA
Kong Kong USA
Mark Lee Korea

Code

<?php

$file = 'file.txt';
$fh = fopen($file, "r");
while ($userinfo = fscanf($fh, "%s\t%s\t%s\n")) {
	echo join(' ',$userinfo);
	echo '<br/>';
}
fclose($fh);
?>

Related Links


basename-- chgrp-- chmod-- chown-- clearstatcache-- copy-- delete-- dirname-- disk_free_space-- disk_total_space-- diskfreespace-- fclose-- feof-- fflush-- fgetc-- fgetcsv-- fgets-- fgetss-- file_exists-- file_get_contents-- file_put_contents- file-- fileatime-- filectime-- filegroup-- fileinode-- filemtime-- fileowner-- fileperms-- filesize-- filetype-- flock-- fnmatch-- fopen-- fpassthru-- fputcsv-- fputs-- fread-- fscanf-- fseek-- fstat-- ftell-- ftruncate-- fwrite-- glob-- is_dir-- is_executable-- is_file-- is_link-- is_readable-- is_uploaded_file-- is_writable-- is_writeable-- lchgrp-- lchown-- link-- linkinfo-- lstat-- mkdir-- move_uploaded_file-- parse_ini_file-- parse_ini_string-- pathinfo-- pclose-- popen-- readfile-- readlink-- realpath_cache_get-- realpath_cache_size-- realpath-- rename-- rewind-- rmdir-- set_file_buffer-- stat-- symlink-- tempnam-- tmpfile-- touch-- umask-- unlink--

Navigation
Web
SQL
MISC
References