PHP parse_ini_file函數(shù)的應(yīng)用與擴(kuò)展操作示例
本文實(shí)例講述了PHP parse_ini_file函數(shù)的應(yīng)用與擴(kuò)展操作。分享給大家供大家參考,具體如下:
parse_ini_file($filename, $process_sections = false, $scanner_mode = INI_SCANNER_NORMAL)解析一個(gè)配置文件。
filename要解析的文件名;
process_sections設(shè)置為true時(shí),得到一個(gè)多維數(shù)組,包括配置文件中每一節(jié)的名稱和設(shè)置,默認(rèn)為false;
解析成功返回關(guān)聯(lián)數(shù)組,失敗返回false。列舉一下官網(wǎng)的例子,也引用了官網(wǎng)的擴(kuò)展實(shí)例parse_ini_file_multi()。
下面是配置文件內(nèi)容:
[first_section]
one = 1
two = 2
name = test
[second_section]
path = '/tmp/test'
url = 'http://test.com/login.php'
[third_section]
php_version[] = '5.0'
php_version[] = '5.1'
php_version[] = '5.5'
[dictionary]
foo[debug] = true
foo[path] = /some/path
[fourth_section]
fold1.fold2.fold3 = 'a'
fold1.fold2.fold4 = 'b'
fold1.fold2.fold5 = 'b'
下面是PHP文件內(nèi)容:
function parse_ini_file_multi($file, $process_sections = false, $scanner_mode = INI_SCANNER_NORMAL){
$explode_str = '.';
$escape_char = "'";
$data = parse_ini_file($file, $process_sections, $scanner_mode);
if (!$process_sections) {
$data = array($data);
}
foreach ($data as $section_key => $section) {
foreach($section as $key => $value){
if(strpos($key, $explode_str)){
if(substr($key, 0, 1) !== $escape_char){
$sub_keys = explode($explode_str, $key);
$subs =& $data[$section_key];
echo "\r\n".'========='."\r\n";
print_r($subs);
print_r($data);
foreach($sub_keys as $sub_key){
if (!isset($subs[$sub_key])) {
$subs[$sub_key] = [];
}
$subs =& $subs[$sub_key];
echo "\r\n".'++++++++'."\r\n";
print_r($subs);
print_r($data);
}
$subs = $value;
echo "\r\n".'----------'."\r\n";
print_r($subs);
print_r($data);
unset($data[$section_key][$key]);
}else{
$new_key = trim($key, $escape_char);
$data[$section_key][$new_key] = $value;
unset($data[$section_key][$key]);
}
}
}
}
if (!$process_sections) {
$data = $data[0];
}
return $data;
}
$arr = parse_ini_file('file.ini');
print_r($arr);
echo "\r\n".'================='."\r\n";
$arr = parse_ini_file_multi('file.ini',true);
echo "\r\n".'================='."\r\n";
print_r($arr);
運(yùn)行結(jié)果:
Array ( [one] => 1 [two] => 2 [name] => test [path] => /tmp/test [url] => http://test.com/login.php [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) [foo] => Array ( [debug] => 1 [path] => /some/path ) [fold1.fold2.fold3] => a [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b ) ================= ========= Array ( [fold1.fold2.fold3] => a [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold3] => a [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b ) ) ++++++++ Array ( ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold3] => a [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( ) ) ) ++++++++ Array ( ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold3] => a [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( ) ) ) ) ++++++++ Array ( ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold3] => a [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => Array ( ) ) ) ) ) ---------- aArray ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold3] => a [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a ) ) ) ) ========= Array ( [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a ) ) ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a ) ) ) ) ++++++++ Array ( [fold2] => Array ( [fold3] => a ) ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a ) ) ) ) ++++++++ Array ( [fold3] => a ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a ) ) ) ) ++++++++ Array ( ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a [fold4] => Array ( ) ) ) ) ) ---------- bArray ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold4] => b [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a [fold4] => b ) ) ) ) ========= Array ( [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a [fold4] => b ) ) ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a [fold4] => b ) ) ) ) ++++++++ Array ( [fold2] => Array ( [fold3] => a [fold4] => b ) ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a [fold4] => b ) ) ) ) ++++++++ Array ( [fold3] => a [fold4] => b ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a [fold4] => b ) ) ) ) ++++++++ Array ( ) Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a [fold4] => b [fold5] => Array ( ) ) ) ) ) ---------- bArray ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1.fold2.fold5] => b [fold1] => Array ( [fold2] => Array ( [fold3] => a [fold4] => b [fold5] => b ) ) ) ) ================= Array ( [first_section] => Array ( [one] => 1 [two] => 2 [name] => test ) [second_section] => Array ( [path] => /tmp/test [url] => http://test.com/login.php ) [third_section] => Array ( [php_version] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.5 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => 1 [path] => /some/path ) ) [fourth_section] => Array ( [fold1] => Array ( [fold2] => Array ( [fold3] => a [fold4] => b [fold5] => b ) ) ) )
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP擴(kuò)展開(kāi)發(fā)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP的mysqli_set_charset()函數(shù)講解
- PHP的mysqli_select_db()函數(shù)講解
- PHP的mysqli_rollback()函數(shù)講解
- php中的explode()函數(shù)實(shí)例介紹
- PHP內(nèi)置函數(shù)生成隨機(jī)數(shù)實(shí)例
- PHP實(shí)現(xiàn)函數(shù)內(nèi)修改外部變量值的方法示例
- php使用array_chunk函數(shù)將一個(gè)數(shù)組分割成多個(gè)數(shù)組
- php圖片裁剪函數(shù)
- PHP格式化顯示時(shí)間date()函數(shù)代碼
- PHP的mysqli_sqlstate()函數(shù)講解
相關(guān)文章
關(guān)于PHP二進(jìn)制流 逐bit的低位在前算法(詳解)
本篇文章是對(duì)PHP二進(jìn)制流逐bit的低位在前算法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHPExcel實(shí)現(xiàn)的讀取多工作表操作示例
這篇文章主要介紹了PHPExcel實(shí)現(xiàn)的讀取多工作表操作,結(jié)合實(shí)例形式分析了PHPExcel針對(duì)Excel多個(gè)sheet工作表的讀取、解析相關(guān)操作技巧,需要的朋友可以參考下2020-04-04
在MongoDB中模擬Auto Increment的php代碼
MySQL用戶多半都有Auto Increment情結(jié),不過(guò)MongoDB缺省并沒(méi)有實(shí)現(xiàn),所以需要模擬一下,編程語(yǔ)言以PHP為例2011-03-03
php采用ajax數(shù)據(jù)提交post與post常見(jiàn)方法總結(jié)
這篇文章主要介紹了php采用ajax數(shù)據(jù)提交post與post常見(jiàn)方法,實(shí)例總結(jié)了json格式傳遞數(shù)據(jù)的優(yōu)點(diǎn)并總結(jié)了四種常見(jiàn)的POST提交數(shù)據(jù)方式,需要的朋友可以參考下2014-11-11
php使用parse_str實(shí)現(xiàn)查詢字符串解析到變量中的方法
這篇文章主要介紹了php使用parse_str實(shí)現(xiàn)查詢字符串解析到變量中的方法,較為詳細(xì)的分析了parse_str()函數(shù)的功能及字符串解析為變量的具體用法,并總結(jié)了使用中的相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-02-02
php中array_pad()函數(shù)用法及實(shí)例
在本篇文章里小編給大家整理的是一篇關(guān)于php中array_pad()函數(shù)用法及實(shí)例內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)下。2021-08-08

