function.parse-ini-file.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 255 行
HTML
255 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Parse a configuration file</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.move-uploaded-file.html">move_uploaded_file</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.pathinfo.html">pathinfo</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.parse-ini-file" class="refentry"> <div class="refnamediv"> <h1 class="refname">parse_ini_file</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">parse_ini_file</span> — <span class="dc-title">Parse a configuration file</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><b><b>parse_ini_file</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$filename</tt></span> [, <span class="methodparam"><span class="type">bool</span> <tt class="parameter">$process_sections</tt></span> ] )</div> <p class="para rdfs-comment"> <b>parse_ini_file()</b> loads in the ini file specified in <i><tt class="parameter">filename</tt></i>, and returns the settings in it in an associative array. </p> <p class="para"> The structure of the ini file is the same as the <var class="filename">php.ini</var>'s. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">filename</tt></i></span> <dd> <p class="para"> The filename of the ini file being parsed. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">process_sections</tt></i></span> <dd> <p class="para"> By setting the last <i><tt class="parameter">process_sections</tt></i> parameter to <b><tt>TRUE</tt></b>, you get a multidimensional array, with the section names and settings included. The default for <i><tt class="parameter">process_sections</tt></i> is <b><tt>FALSE</tt></b> </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> The settings are returned as an associative <a href="language.types.array.html" class="type array">array</a> on success, and <b><tt>FALSE</tt></b> on failure. </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">5.2.7</td> <td colspan="1" rowspan="1" align="left"> On syntax error this function will return <b><tt>FALSE</tt></b> rather then an empty array </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">5.2.4</td> <td colspan="1" rowspan="1" align="left"> Keys and section names consisting of numbers are now evaluated as PHP <a href="language.types.integer.html" class="link">integers</a> thus numbers starting by 0 are evaluated as octals and numbers starting by 0x are evaluated as hexadecimals. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">5.0.0</td> <td colspan="1" rowspan="1" align="left"> Values enclosed in double quotes can contain new lines. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.2.1</td> <td colspan="1" rowspan="1" align="left"> This function is now affected by <a href="features.safe-mode.html#ini.safe-mode" class="link">safe mode</a> and <a href="features.safe-mode.html#ini.open-basedir" class="link">open_basedir</a>. </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 Contents of <var class="filename">sample.ini</var></b></p> <div class="example-contents"><div class="cdata"><pre>; This is a sample configuration file; Comments start with ';', as in php.ini[first_section]one = 1five = 5animal = BIRD[second_section]path = "/usr/local/bin"URL = "http://www.example.com/~username"</pre></div> </div> </div> <div class="example"> <p><b>Example #2 <b>parse_ini_file()</b> example</b></p> <div class="example-contents"><p> <a href="language.constants.html" class="link">Constants</a> may also be parsed in the ini file so if you define a constant as an ini value before running <b>parse_ini_file()</b>, it will be integrated into the results. Only ini values are evaluated. For example: </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br />define</span><span style="color: #007700">(</span><span style="color: #DD0000">'BIRD'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Dodo bird'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Parse without sections<br /></span><span style="color: #0000BB">$ini_array </span><span style="color: #007700">= </span><span style="color: #0000BB">parse_ini_file</span><span style="color: #007700">(</span><span style="color: #DD0000">"sample.ini"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$ini_array</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Parse with sections<br /></span><span style="color: #0000BB">$ini_array </span><span style="color: #007700">= </span><span style="color: #0000BB">parse_ini_file</span><span style="color: #007700">(</span><span style="color: #DD0000">"sample.ini"</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$ini_array</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>Array( [one] => 1 [five] => 5 [animal] => Dodo bird [path] => /usr/local/bin [URL] => http://www.example.com/~username)Array( [first_section] => Array ( [one] => 1 [five] => 5 [animal] = Dodo bird ) [second_section] => Array ( [path] => /usr/local/bin [URL] => http://www.example.com/~username ))</pre></div> </pre></div> </div> </p> </div> <div class="refsect1 notes"> <h3 class="title">Notes</h3> <blockquote><p><b class="note">Note</b>: This function has nothing to do with the <var class="filename">php.ini</var> file. It is already processed, the time you run your script. This function can be used to read in your own application's configuration files. <br /> </p></blockquote> <blockquote><p><b class="note">Note</b>: If a value in the ini file contains any non-alphanumeric characters it needs to be enclosed in double-quotes ("). <br /> </p></blockquote> <blockquote><p><b class="note">Note</b>: <span class="simpara"> There are reserved words which must not be used as keys for ini files. These include: null, yes, no, true, and false. Values null, no and false results in "", yes and true results in "1". Characters <i>{}|&~![()"</i> must not be used anywhere in the key and have a special meaning in the value. </span> </p></blockquote> </div> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.move-uploaded-file.html">move_uploaded_file</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.pathinfo.html">pathinfo</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 + =
减小字号Ctrl + -
显示快捷键?