php parse_ini_file

<h1 >parse_ini_file</h1> <div class="refnamediv" > <p>(PHP 4, PHP 5)</p> parse_ini_file--解析一个配置文件</div> <div class="refsect1" > <h2>说明</h2> array<span class="Apple-converted-space"></span><b class="methodname">parse_ini_file</b><span class="Apple-converted-space"></span>( string filename [, bool process_sections] )<br /> <br /> <p><b class="function">parse_ini_file()</b><span class="Apple-converted-space"></span>载入一个由<span class="Apple-converted-space"></span><var class="parameter">filename</var><span class="Apple-converted-space"></span>指定的 ini 文件,并将其中的设置作为一个联合数组返回。如果将最后的<span class="Apple-converted-space"></span><var class="parameter">process_sections</var><span class="Apple-converted-space"></span>参数设为<span class="Apple-converted-space"></span><tt class="constant"><b><font face="NSimsun">TRUE</font></b></tt>,将得到一个多维数组,包括了配置文件中每一节的名称和设置。<var class="parameter">process_sections</var><span class="Apple-converted-space"></span>的默认值是<span class="Apple-converted-space"></span><tt class="constant"><b><font face="NSimsun">FALSE</font></b></tt>。</p> <div class="note"><blockquote class="note"> <p><b>注:<span class="Apple-converted-space"></span></b>本函数和<span class="Apple-converted-space"></span><tt class="filename"><font face="NSimsun">php.ini</font></tt><span class="Apple-converted-space"></span>文件没有关系,该文件在运行脚本时就已经处理过了。本函数可以用来读取你自己的应用程序的配置文件。</p> </blockquote></div> <div class="note"><blockquote class="note"> <p><b>注:<span class="Apple-converted-space"></span></b>如果 ini 文件中的值包含任何非字母数字的字符,需要将其括在双引号中(&quot;)。</p> </blockquote></div> <div class="note"><blockquote class="note"> <p><b>注:<span class="Apple-converted-space"></span></b>自 PHP 4.2.1 其本函数也受到安全模式和<span class="Apple-converted-space"></span>open_basedir<span class="Apple-converted-space"></span>的影响。</p> </blockquote></div> <div class="note"><blockquote class="note"> <p><b>注:<span class="Apple-converted-space"></span></b>自 PHP 5.0 版本开始,该函数也处理选项值内的新行。</p> </blockquote></div> <div class="note"><blockquote class="note"> <p><b>注:<span class="Apple-converted-space"></span></b>有些保留字不能作为 ini 文件中的键名,包括:null,yes,no,true 和 false。值为 null,no 和 false 等效于 &quot;&quot;,值为 yes 和 true 等效于 &quot;1&quot;。字符<span class="Apple-converted-space"></span><var class="literal">|&amp;~![()&quot;</var><span class="Apple-converted-space"></span>也不能用在键名的任何地方,而且这些字符在选项值中有着特殊的意义。</p> </blockquote></div> <p>ini 文件的结构和<span class="Apple-converted-space"></span><tt class="filename"><font face="NSimsun">php.ini</font></tt><span class="Apple-converted-space"></span>的相似。</p> <p>常量也可以在 ini 文件中被解析,因此如果在运行<span class="Apple-converted-space"></span><b class="function">parse_ini_file()</b><span class="Apple-converted-space"></span>之前定义了常量作为 ini 的值,将会被集成到结果中去。只有 ini 的值会被求值。例如:</p> <p> <table class="EXAMPLE" border="0" cellspacing="0" cellpadding="0" width="100%" > <tbody> <tr> <td > <div class="example"> <p><b>例子 1.<span class="Apple-converted-space"></span><tt class="filename"><font face="NSimsun">sample.ini</font></tt><span class="Apple-converted-space"></span>的内容</b></p> <table border="0" cellpadding="5" bgcolor="#e0e0e0" > <tbody> <tr> <td > <pre class="programlisting"> ; This is a sample configuration file ; Comments start with ';', as in php.ini [first_section] one = 1 five = 5 animal = BIRD [second_section] path = /usr/local/bin URL = &quot;http://www.example.com/~username&quot;</pre> </td> </tr> </tbody> </table> </div> </td> </tr> </tbody> </table> </p> <p> <table class="EXAMPLE" border="0" cellspacing="0" cellpadding="0" width="100%" > <tbody> <tr> <td > <div class="example"> <p><b>例子 2.<span class="Apple-converted-space"></span><b class="function">parse_ini_file()</b><span class="Apple-converted-space"></span>例子</b></p> <table border="0" cellpadding="5" bgcolor="#e0e0e0" > <tbody> <tr> <td ><code><font color="#000000"><font face="NSimsun"><font color="#0000bb">&lt;?php<br /> <br /> define</font><font color="#007700">(</font><font color="#dd0000">'BIRD'</font><font color="#007700">,<span class="Apple-converted-space"></span></font><font color="#dd0000">'Dodo bird'</font></font><font color="#007700" face="NSimsun">);<br /> <br /> </font><font face="NSimsun"><font color="#ff8000">// Parse without sections<br /> </font><font color="#0000bb">$ini_array<span class="Apple-converted-space"></span></font><font color="#007700">=<span class="Apple-converted-space"></span></font><font color="#0000bb">parse_ini_file</font><font color="#007700">(</font><font color="#dd0000">&quot;sample.ini&quot;</font></font><font face="NSimsun"><font color="#007700">);<br /> </font><font color="#0000bb">print_r</font><font color="#007700">(</font><font color="#0000bb">$ini_array</font></font><font color="#007700" face="NSimsun">);<br /> <br /> </font><font face="NSimsun"><font color="#ff8000">// Parse with sections<br /> </font><font color="#0000bb">$ini_array<span class="Apple-converted-space"></span></font><font color="#007700">=<span class="Apple-converted-space"></span></font><font color="#0000bb">parse_ini_file</font><font color="#007700">(</font><font color="#dd0000">&quot;sample.ini&quot;</font><font color="#007700">,<span class="Apple-converted-space"></span></font><font color="#0000bb">true</font></font><font face="NSimsun"><font color="#007700">);<br /> </font><font color="#0000bb">print_r</font><font color="#007700">(</font><font color="#0000bb">$ini_array</font></font><font face="NSimsun"><font color="#007700">);<br /> <br /> </font><font color="#0000bb">?&gt;</font></font></font></code></td> </tr> </tbody> </table> <p>上例将输出:</p> <table border="0" cellpadding="5" bgcolor="#e0e0e0" > <tbody> <tr> <td > <pre class="screen"> Array ( [one] =&gt; 1 [five] =&gt; 5 [animal] =&gt; Dodo bird [path] =&gt; /usr/local/bin [URL] =&gt; http://www.example.com/~username ) Array ( [first_section] =&gt; Array ( [one] =&gt; 1 [five] =&gt; 5 [animal] = Dodo bird ) [second_section] =&gt; Array ( [path] =&gt; /usr/local/bin [URL] =&gt; http://www.example.com/~username ) )</pre> </td> </tr> </tbody> </table> </div> </td> </tr> </tbody> </table> </p> <p>由数字组成的键名和小节名会被 PHP 当作整数来处理,因此以 0 开头的数字会被当作八进制而以 0x 开头的会被当作十六进制。</p> </div> <p ><br /> </p>
RangeTime:0.006861s
RangeMem:211.54 KB
返回顶部 留言