PHP: fscanf - Manual (original) (raw)

(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)

fscanf — Parses input from a file according to a format

Description

Any whitespace in the format string matches any whitespace in the input stream. This means that even a tab (\t) in the format string can match a single space character in the input stream.

Each call to fscanf() reads one line from the file.

Parameters

stream

A file system pointer resourcethat is typically created using fopen().

format

The interpreted format for string, which is described in the documentation for sprintf() with following differences:

vars

The optional assigned values.

Return Values

If only two parameters were passed to this function, the values parsed will be returned as an array. Otherwise, if optional parameters are passed, the function will return the number of assigned values. The optional parameters must be passed by reference.

If there are more substrings expected in the format than there are available within string,[null](reserved.constants.php#constant.null) will be returned. On other errors, [false](reserved.constants.php#constant.false) will be returned.

Examples

Example #1 fscanf() Example

<?php $handle = fopen("users.txt", "r"); while ($userinfo = fscanf($handle, "%s\t%s\t%s\n")) { list ($name, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>p</mi><mi>r</mi><mi>o</mi><mi>f</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">profession, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">p</span><span class="mord mathnormal">ro</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">ess</span><span class="mord mathnormal">i</span><span class="mord mathnormal">o</span><span class="mord mathnormal">n</span><span class="mpunct">,</span></span></span></span>countrycode) = $userinfo; //... do something with the values } fclose($handle); ?>

Example #2 Contents of users.txt

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

See Also

Found A Problem?

yasuo_ohgaki at hotmail dot com

24 years ago

For C/C++ programmers.

fscanf() does not work like C/C++, because PHP's fscanf() move file pointer the next line implicitly.

Bertrand dot Lecun at prism dot uvsq dot Fr

18 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.

eugene at pro-access dot com

23 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.

worldwideroach at hotmail dot com

20 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, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>D</mi><mi>e</mi><mi>l</mi><mi>i</mi><mi>m</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">sDelim, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">sDe</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">im</span><span class="mpunct">,</span></span></span></span>iBuffer=1024) {
    $sRecord = '';
    while(!feof($rFile)) {
        <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mi>P</mi><mi>o</mi><mi>s</mi><mo>=</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>p</mi><mi>o</mi><mi>s</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">iPos = strpos(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mord mathnormal">os</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">p</span><span class="mord mathnormal">os</span><span class="mopen">(</span></span></span></span>sRecord, $sDelim);
        if ($iPos === false) {
            <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>R</mi><mi>e</mi><mi>c</mi><mi>o</mi><mi>r</mi><mi>d</mi><mi mathvariant="normal">.</mi><mo>=</mo><mi>f</mi><mi>r</mi><mi>e</mi><mi>a</mi><mi>d</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">sRecord .= fread(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal" style="margin-right:0.00773em;">R</span><span class="mord mathnormal" style="margin-right:0.02778em;">ecor</span><span class="mord mathnormal">d</span><span class="mord">.</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">re</span><span class="mord mathnormal">a</span><span class="mord mathnormal">d</span><span class="mopen">(</span></span></span></span>rFile, $iBuffer);
        } else {
            fseek($rFile, 0-strlen($sRecord)+$iPos+strlen($sDelim), SEEK_CUR);
            return substr($sRecord, 0, $iPos);
        }
    }
    return false;
}

nico at nicoswd dot com

12 years ago

If you want to parse a cron file, you may use this pattern:

<?php

while ($cron = fscanf($fp, "%s %s %s %s %s %[^\n]s"))
{

}

?>

loco.xxx at gmail dot com

19 years ago

to include all type of visible chars you should try:

<?php fscanf($file_handler,"%[ -~]"); ?>

robert at NOSPAM dot NOSPAM

23 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.