1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Function about ini file read, write and modify. |
4
|
|
|
* |
5
|
|
|
* @package fwolflib |
6
|
|
|
* @subpackage func |
7
|
|
|
* @copyright Copyright 2008-2010, Fwolf |
8
|
|
|
* @author Fwolf <[email protected]> |
9
|
|
|
* @since 2008-04-23 |
10
|
|
|
* @link http://www.php.net/manual/en/function.parse-ini-file.php |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
require_once(dirname(__FILE__) . '/../fwolflib.php'); |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Read ini file |
19
|
|
|
* |
20
|
|
|
* To retrieve global value, set $section to ' ' instead of ''. |
21
|
|
|
* @param string $filepath |
22
|
|
|
* @param string $section If obmit, return array while each index is a section name. |
23
|
|
|
* @param string $item If obmit, return array include all items. |
24
|
|
|
* @return mixed |
25
|
|
|
*/ |
26
|
|
|
function IniGet($filepath, $section = '', $item = '') |
27
|
|
|
{ |
28
|
|
|
$ini = file($filepath); |
29
|
|
|
if (empty($ini)) |
30
|
|
|
return ''; |
31
|
|
|
|
32
|
|
|
/* |
33
|
|
|
Result array should like this: |
34
|
|
|
array( |
35
|
|
|
global, |
36
|
|
|
section => array( |
37
|
|
|
item => value |
38
|
|
|
) |
39
|
|
|
) |
40
|
|
|
*/ |
41
|
|
|
$rs = array(); |
42
|
|
|
|
43
|
|
|
$i = 0; |
44
|
|
|
$s_section = ''; // Current scaning section name |
45
|
|
|
foreach ($ini as $line) |
46
|
|
|
{ |
47
|
|
|
$line = trim($line); |
48
|
|
|
// Empyt line |
49
|
|
|
if (empty($line)) continue; |
50
|
|
|
// Comments |
51
|
|
|
if ((';' == $line{0}) || ('#' == $line{0})) |
52
|
|
|
continue; |
53
|
|
|
|
54
|
|
|
// Section |
55
|
|
|
if ('[' == $line{0}) |
56
|
|
|
{ |
57
|
|
|
$s_section = substr($line, 1, -1); |
58
|
|
|
$i++; |
59
|
|
|
continue; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Key-value pair |
63
|
|
|
list($key, $value) = explode('=', $line, 2); |
64
|
|
|
$key = trim($key); |
65
|
|
|
$value = trim($value); |
66
|
|
|
if (0 == $i) |
67
|
|
|
{ |
68
|
|
|
// Global value |
69
|
|
View Code Duplication |
if ('[]' == substr($line, -1, 2)) |
|
|
|
|
70
|
|
|
// Array value in ini file ? |
71
|
|
|
$rs[' '][$key][] = $value; |
72
|
|
|
else |
73
|
|
|
$rs[' '][$key] = $value; |
74
|
|
|
} |
75
|
|
View Code Duplication |
else |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
// Section value |
78
|
|
|
if ('[]' == substr($line, -1, 2)) |
79
|
|
|
$rs[$s_section][$key][] = $value; |
80
|
|
|
else |
81
|
|
|
$rs[$s_section][$key] = $value; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Return values by param |
86
|
|
|
if (empty($section)) |
87
|
|
|
return $rs; |
88
|
|
|
elseif (empty($item)) |
89
|
|
|
{ |
90
|
|
|
if (isset($rs[$section])) |
91
|
|
|
return $rs[$section]; |
92
|
|
|
else |
93
|
|
|
return ''; |
94
|
|
|
} |
95
|
|
|
else |
96
|
|
|
{ |
97
|
|
|
if (isset($rs[$section][$item])) |
98
|
|
|
return $rs[$section][$item]; |
99
|
|
|
else |
100
|
|
|
return ''; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} // end of func IniGet |
104
|
|
|
|
105
|
|
|
?> |
|
|
|
|
106
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.