downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

fseek> <fread
[edit] Last updated: Fri, 17 May 2013

view this page in

fscanf

(PHP 4 >= 4.0.1, PHP 5)

fscanfInterpretiert den Input einer Datei entsprechend einem angegebenen Format

Beschreibung

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

Die Funktion fscanf() ist ähnlich zu sscanf(), wobei sie ihren Input aus der mit handle angegebenen Datei liest, und entsprechend dem angegebenen format interpretiert. Der format Parameter is unter der sprintf() Funktion näher dokumentiert.

Beliebige Whitespace-Zeichen (z.B. Leerzeichen, Tabulator, etc.) im Format String gelten mit beliebigen Whitespace-Zeichen des Input-Streams als übereinstimmend. Das heißt, dass auch ein Tabulator \t im Format String mit einem einzigen Leerzeichen im Input-Stream als übereinstimmend gelten kann.

Jeder fscanf() Aufruf liest eine Zeile aus der Datei aus.

Parameter-Liste

handle

Eine Dateisystemressource (resource), wie sie in der Regel von fopen() zurückgegeben wird.

format

Das erwartete Format, wie es in der sprintf() Dokumentation beschrieben ist.

...

Optionale als Referenz übergebene Variablen, in welche die geparsten Werte geschrieben werden sollen.

Rückgabewerte

Wenn nur zwei Parameter an die Funktion übergeben wurden, werden die geparseten Werte in einem Array zurückgegeben. Andernfalls gibt die Funktion die anzahl der zugewiesenen Werte zurück. Die optionalen Parameter müssen per Referenz übergeben werden.

Changelog

Version Beschreibung
4.3.0 In früheren Versionen wurden maximal 512 Zeichen pro Aufruf eingelesen. Ab PHP 4.3.0 werden beliebig lange Zeile eingelesen und interpretiert.

Beispiele

Beispiel #1 fscanf() Beispiel

<?php
$handle 
fopen("users.txt""r");
while (
$userinfo fscanf($handle"%s\t%s\t%s\n")) {
    list (
$name$profession$countrycode) = $userinfo;
    
// Tue etwas mit den Werten ...
}
fclose($handle);
?>

Beispiel #2 Inhalt der users.txt Datei

javier  argonaut        pe
hiroshi sculptor        jp
robert  slacker us
luigi   florist it

Siehe auch

  • fread() - Liest Binärdaten aus einer Datei
  • fgets() - Liest eine Zeile von der Position des Dateizeigers
  • fgetss() - Liest eine Zeile von der Position des Dateizeigers und entfernt HTML Tags.
  • sscanf() - Überträgt einen String in ein angegebenes Format
  • printf() - Gibt einen formatierten String aus
  • sprintf() - Gibt einen formatierten String zurück



fseek> <fread
[edit] Last updated: Fri, 17 May 2013
 
add a note add a note User Contributed Notes fscanf - [14 notes]
up
1
arentzen at religion dot dk
5 years ago
If you want fscanf()to scan one variable in a large number of lines,  e.g an Ipadress in a line with more variables, then use fscanf with explode()
<?
$filename = "somefile.txt";
$fp = fopen($filename, "r") or die ("Error opening file! \n");
$u = explode(" ",$line); // $u is the variable eg. an IPadress
while ($line = fscanf($fp,"%s",$u)) {
if(preg_match("/^$u/",$_SERVER['REMOTE_ADDR'])) {$badipadresss++;} // do something and continue scan
}
?>
Besides, fscanf()is much faster than fgets()
up
1
eugene at pro-access dot com
11 years ago
If you want to read text files in csv format or the like(no matter what character the fields are separated with), you should use fgetcsv() instead. When a text for a field is blank, fscanf() may skip it and fill it with the next text, whereas fgetcsv() correctly regards it as a blank field.
up
1
Bertrand dot Lecun at prism dot uvsq dot Fr
6 years ago
It would be great to precise in the fscanf documentation
that one call to the function, reads a complete line.
and not just the number of values defined in the format.

If a text file contains 2 lines each containing 4 integer values,
reading the file with 8 fscanf($fd,"%d",$v) doesnt run !
You have to make 2
fscanf($fd,"%d %d %d %d",$v1,$v2,$v3,$v4);

Then 1 fscanf per line.
up
0
yasuo_ohgaki at hotmail dot com
12 years ago
For C/C++ programmers.

fscanf() does not work like C/C++, because PHP's fscanf() move file pointer the next line implicitly.
up
0
loco.xxx at gmail dot com
6 years ago
to include all type of visible chars you should try:

<?php fscanf($file_handler,"%[ -~]"); ?>
up
0
worldwideroach at hotmail dot com
7 years ago
Yet another function to read a file and return a record/string by a delimiter.  It is very much like fgets() with the delimiter being an additional parameter.  Works great across multiple lines.

function fgetd(&$rFile, $sDelim, $iBuffer=1024) {
    $sRecord = '';
    while(!feof($rFile)) {
        $iPos = strpos($sRecord, $sDelim);
        if ($iPos === false) {
            $sRecord .= fread($rFile, $iBuffer);
        } else {
            fseek($rFile, 0-strlen($sRecord)+$iPos+strlen($sDelim), SEEK_CUR);
            return substr($sRecord, 0, $iPos);
        }
    }
    return false;
}
up
0
robert at NOSPAM dot NOSPAM
10 years ago
actually, instead of trying to think of every character that might be in your file, excluding the delimiter would be much easier.

for example, if your delimiter was a comma use:

%[^,]

instead of:

%[a-zA-Z0-9.| ... ]

Just make sure to use %[^,\n] on your last entry so you don't include the newline.
up
0
james at zephyr-works dot com
11 years ago
fscanf works a little retardedly I've found. Instead of using just a plain %s you probably will need to use sets instead. Because it works so screwy compared to C/C++, fscanf does not have the ability to scan ahead in a string and pattern match correctly, so a seemingly perfect function call like:

fscanf($fh, "%s::%s");

With a file like:

user::password

Will not work. When fscanf looks for a string, it will look and stop at nothing except for a whitespace so :: and everything except whitespace is considered part of that string, however you can make it a little smarter by:

fscanf($fh, "%[a-zA-Z0-9,. ]::%[a-zA-Z0-9,. ]" $var1, $var2);

Which tells it that it can only accept a through z A through Z 0 through 9 a comma a period and a whitespace as input to the string, everything else cause it to stop taking in as input and continue parsing the line. This is very useful if you want to get a sentence into the string and you're not sure of exactly how many words to add, etc.
up
-1
rudigreen at gmail dot com
7 years ago
I have a function for reading delimited files, it works for multiple lines too (i think...)

<?
//$fh - is the file pointer
//$delim - is the seperator
//$callback - self explanatory
//$len - optional
function file_read_delim($fh,$delim,$callback,$len=1024)
{
    $rec = '';
    while(!feof($fh))
    {
        $buf = fread($fh,$len);
        if(strpos($buf,$delim) === false)
        {
            $rec .= $buf;
        }
        else
        {
            $strs = explode($delim,$buf);
            foreach ($strs as $ele)
            {
                $rec .= $ele;
                call_user_func($callback,$rec);
                $rec = '';
            }
        }
    }
}

//Here is an example how to use the function

$fh = fopen($filename,'r');
    if(!$fh)
    {
                 die 'Could not open file for reading';
    }
        //call the function
    file_read_delim($fh,'-','cb');
    fclose($fh);

function cb($rec)
{
echo "$rec \n";
}
?>
up
-1
me at hesterc dot fsnet dot co dot uk
9 years ago
I have a simpler method I use to parse delimited text. Using the data posted by gozer at fanhunter dot com, here is my script. Maybe it is faster?

<?php

$fp
= fopen ("sections.dat","r");

if (!
$fp) {echo "<p>Unable to open remote file.</p>"; exit;}

while (!
feof($fp)):
 
$line = fgets($fp, 2048);
 
$out = array($line);
 list (
$id, $name, $description, $language, $directory, $id_uplevel, $order, $hassubsection) = split ("\|", $out[0]);
 echo
"$id-$name-$description-$language-$directory-
$id_uplevel-$order-$hassubsection<br />\n";
 
$fp++;
endwhile;

fclose($fp);
?>

Notes:

Avoid the php extension on a data file - it will cause PHP to parse the file, but there is no PHP in it.

The "2048" value on line 2 of the loop is set for long lines. 1024 works fine, but I had to increase it with a large database I use a similar script to read.

You don't need to open and close the speech marks (as in gozer at fanhunter dot com's example) in the echo line, just use the variables inbetween the dashes.

(Remove the line break halfway through the echo line - it is just there for this forum.)
up
-1
matt at mattsinclair dot com
9 years ago
A better way to use fscanf() would be this:

<?php
$handle
= fopen("users.txt", "r");
while (!
feof($handle)) {
  
$userinfo = fscanf($handle, "%s\t%s\t%s\n");
   if (
$userinfo) {
     list (
$name, $profession, $countrycode) = $userinfo;
    
//... do something with the values
  
}
  
$userinfo=NULL;
}
fclose($handle);
?>

as you can see, instead of waiting for fscanf() to fail to return a value... it waits for the the pointer to get to the end of the file... this way, if for some reason one of your lines does not match your expression, it will not kill the loop.  it will simply go on to the next line.
up
-1
gozer at fanhunter dot com
11 years ago
Hi,
A few days ago we got multiple mySQL crashes due to a hardware failure and other processes running.
While we thought it could be the mySQL daemon overloaded, we started looking for alternate ways to get our little databases working so we started using fscanf to parse files.

We ran into multiple problems due to the whitespace and other characters that were in our database. Finally, we made it to work using sets as james@zephyr-works.com remarked.

Our final function is:

  function get_sections($include_dir){
    $filename = $include_dir . "sections.dat.php";
    $datafile = fopen ($filename ,"r");
    while ($sectioninfo = fscanf ($datafile, "%[0-9]|%[a-zA-Z0-9@&;:,. /!?-]|%[a-zA-Z0-9@&;:,. /!?-]|%[a-zA-Z]|%[a-zA-Z0-9@/?&;.+=-]|%[0-9]|%[0-9]|%[0-9]\n")) {
        list($id, $name, $description, $language, $directory, $id_uplevel, $order, $hassubsection) = $sectioninfo;

        // Show output
        echo $id . "-" . $name. "-" . $description . "-" . $language . "-" . $directory . "-" . $id_uplevel . "-" . $order . "-" . $hassubsection . "<br>\n";
    }
    fclose($datafile);
  }

The contents of sections.dat.php (for example):

1|home|P&aacute;gina principal de Fanhunter.|castellano|==|0|0|0
2|fanhunter|Secci&oacute;n principal dedicada al universo Fanhunter.|castellano|fanhunter/|1|0|0
3|outfan|Secci&oacute;n principal dedicada al universo Outfan.|castellano|outfan/|1|0|0
4|fanpiro|Secci&oacute;n principal dedicada al universo Fanpiro.|castellano|fanpiro/|1|0|0
5|tienda|La tienda de Fanhunter.|castellano|tienda/|1|0|0
6|the zone|Secci&oacute;n principal Miscel&aacute;nea.|castellano|thezone/|1|0|0
7|flfcn|Secci&oacute;n principal dedicada a Fan Letal/Fan con Nata.|castellano|fanletal/|1|0|0
8|foro|Nuestro foro de discusi&oacute;n.|castellano|foro/|1|0|0
9|chat|Secci&oacute;n para chatear.|castellano|chat/|1|0|0
10|links|Secci&oacute;n recopilatoria de enlaces de inter&eacute;s a otras p&aacute;ginas.|castellano|links/|1|0|0

Note: The '==' in directory means no directory needed to be specified.
Pay attention to linebreaks, as this forum puts some of them into the code I pasted.

Good luck guys.
up
-2
dave at dave dot st
4 years ago
The fgetd() function suggested by worldwideroach on 14-Jul-2005 04:33 does not handle the last buffer-load of data correctly. At least not for my requirement. It is possible for EOF to have been reached but for there still to be characters in $sRecord.

For the, er... record I got better results with this version:

<?php
function fgetd(&$rFile, $sDelim, $iBuffer=1024) {

 
$sRecord = '';

  while(!
feof($rFile)) {
   
$iPos = strpos($sRecord, $sDelim);
    if (
$iPos === FALSE) {
       
$sRecord .= fread($rFile, $iBuffer);
    } else {
       
fseek($rFile, 0-strlen($sRecord)+$iPos+strlen($sDelim), SEEK_CUR);
        return
substr($sRecord, 0, $iPos);
    }
  }

 
// Last read got some more data before hitting EOF?
 
if ($sRecord != '') {
    if ((
$iPos = strpos($sRecord, $sDelim)) !== FALSE) {
     
fseek($rFile, 0-strlen($sRecord)+$iPos+strlen($sDelim), SEEK_CUR);
      return
substr($sRecord, 0, $iPos);
    }
    else {
      return
$sRecord;
    }
  }
  else {
    return
FALSE;
  }
}
?>
up
-2
ruiner911 at yahoo dot com
10 years ago
Clear the variables before you scan them in.  As a programmer this should have been very apparent.  Goof.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites