<HTML ><HEAD ><TITLE >fscanf</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Filesystem functions" HREF="ref.filesystem.html"><LINK REL="PREVIOUS" TITLE="fread" HREF="function.fread.html"><LINK REL="NEXT" TITLE="fseek" HREF="function.fseek.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.fread.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.fseek.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.fscanf" ></A >fscanf</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN26069" ></A ><P > (PHP 4 >= 4.0.1)</P >fscanf -- Parses input from a file according to a format</DIV ><DIV CLASS="refsect1" ><A NAME="AEN26072" ></A ><H2 >Description</H2 >mixed <B CLASS="methodname" >fscanf</B > ( resource handle, string format [, string var1])<BR ></BR ><P > The function <B CLASS="function" >fscanf()</B > is similar to <A HREF="function.sscanf.html" ><B CLASS="function" >sscanf()</B ></A >, but it takes its input from a file associated with <TT CLASS="parameter" ><I >handle</I ></TT > and interprets the input according to the specified <TT CLASS="parameter" ><I >format</I ></TT >. 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. </P ><P > Any whitespace in the format string matches any whitespace in the input stream. This means that even a tab <TT CLASS="literal" >\t</TT > in the format string can match a single space character in the input stream. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN26094" ></A ><P ><B >Example 1. <B CLASS="function" >fscanf()</B > Example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$handle = fopen ("users.txt","r"); while ($userinfo = fscanf ($handle, "%s\t%s\t%s\n")) { list ($name, $profession, $countrycode) = $userinfo; //... do something with the values } fclose($handle);</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN26098" ></A ><P ><B >Example 2. users.txt</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >javier argonaut pe hiroshi sculptor jp robert slacker us luigi florist it</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > Prior to PHP 4.3.0, the maximum number of characters read from the file was 512 (or up to the first \n, whichever came first). As of PHP 4.3.0 arbitrarily long lines will be read and scanned. </P ></BLOCKQUOTE ></DIV ><P > See also <A HREF="function.fread.html" ><B CLASS="function" >fread()</B ></A >, <A HREF="function.fgets.html" ><B CLASS="function" >fgets()</B ></A >, <A HREF="function.fgetss.html" ><B CLASS="function" >fgetss()</B ></A >, <A HREF="function.sscanf.html" ><B CLASS="function" >sscanf()</B ></A >, <A HREF="function.printf.html" ><B CLASS="function" >printf()</B ></A >, and <A HREF="function.sprintf.html" ><B CLASS="function" >sprintf()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.fread.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.fseek.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >fread</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.filesystem.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >fseek</TD ></TR ></TABLE ></DIV ></BODY ></HTML >