📄 function.fscanf.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Parses input from a file according to a format</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.fread.html">fread</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fseek.html">fseek</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.fscanf" class="refentry"> <div class="refnamediv"> <h1 class="refname">fscanf</h1> <p class="verinfo">(PHP 4 >= 4.0.1, PHP 5)</p><p class="refpurpose"><span class="refname">fscanf</span> — <span class="dc-title">Parses input from a file according to a format</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><b><b>fscanf</b></b></span> ( <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$handle</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$format</tt></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter reference">&$...</tt></span> ] )</div> <p class="para rdfs-comment"> The function <b>fscanf()</b> is similar to <a href="function.sscanf.html" class="function">sscanf()</a>, but it takes its input from a file associated with <i><tt class="parameter">handle</tt></i> and interprets the input according to the specified <i><tt class="parameter">format</tt></i>, which is described in the documentation for <a href="function.sprintf.html" class="function">sprintf()</a>. </p> <p class="para"> Any whitespace in the format string matches any whitespace in the input stream. This means that even a tab <i>\t</i> in the format string can match a single space character in the input stream. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">handle</tt></i></span> <dd> <p class="para">A file system pointer <a href="language.types.resource.html" class="type resource">resource</a>that is typically created using <a href="function.fopen.html" class="function">fopen()</a>.</p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">format</tt></i></span> <dd> <p class="para"> The specified format as described in the <a href="function.sprintf.html" class="function">sprintf()</a> documentation. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">...</tt></i></span> <dd> <p class="para"> The optional assigned values. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> 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> </div> <div class="refsect1 changelog"> <h3 class="title">ChangeLog</h3> <p class="para"> <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.3.0</td> <td colspan="1" rowspan="1" align="left"> Before this time, the maximum number of characters read from the file was 512 (or up to the first \n, whichever came first). But now, arbitrarily long lines will be read and scanned. </td> </tr> </tbody> </colgroup> </table> </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>fscanf()</b> Example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"users.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br />while (</span><span style="color: #0000BB">$userinfo </span><span style="color: #007700">= </span><span style="color: #0000BB">fscanf</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">, </span><span style="color: #DD0000">"%s\t%s\t%s\n"</span><span style="color: #007700">)) {<br /> list (</span><span style="color: #0000BB">$name</span><span style="color: #007700">, </span><span style="color: #0000BB">$profession</span><span style="color: #007700">, </span><span style="color: #0000BB">$countrycode</span><span style="color: #007700">) = </span><span style="color: #0000BB">$userinfo</span><span style="color: #007700">;<br /> </span><span style="color: #FF8000">//... do something with the values<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #2 Contents of users.txt</b></p> <div class="example-contents"><div class="cdata"><pre>javier argonaut pehiroshi sculptor jprobert slacker usluigi florist it</pre></div> </div> </div> </p> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.fread.html" class="function" rel="rdfs-seeAlso">fread()</a></li> <li class="member"><a href="function.fgets.html" class="function" rel="rdfs-seeAlso">fgets()</a></li> <li class="member"><a href="function.fgetss.html" class="function" rel="rdfs-seeAlso">fgetss()</a></li> <li class="member"><a href="function.sscanf.html" class="function" rel="rdfs-seeAlso">sscanf()</a></li> <li class="member"><a href="function.printf.html" class="function" rel="rdfs-seeAlso">printf()</a></li> <li class="member"><a href="function.sprintf.html" class="function" rel="rdfs-seeAlso">sprintf()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.fread.html">fread</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fseek.html">fseek</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -