आप, यह INI फ़ाइल पढ़ता है, और लिखने पर सेटिंग्स को बरकरार रखता है इस से शुरू करने का प्रयास, आप नए प्रविष्टियों को जोड़ने का समर्थन करने के लिए इसे विस्तार करने के लिए होगा सकता है:
class ini {
protected $lines;
public function read($file) {
$this->lines = array();
$section = '';
foreach(file($file) as $line) {
// comment or whitespace
if(preg_match('/^\s*(;.*)?$/', $line)) {
$this->lines[] = array('type' => 'comment', 'data' => $line);
// section
} elseif(preg_match('/\[(.*)\]/', $line, $match)) {
$section = $match[1];
$this->lines[] = array('type' => 'section', 'data' => $line, 'section' => $section);
// entry
} elseif(preg_match('/^\s*(.*?)\s*=\s*(.*?)\s*$/', $line, $match)) {
$this->lines[] = array('type' => 'entry', 'data' => $line, 'section' => $section, 'key' => $match[1], 'value' => $match[2]);
}
}
}
public function get($section, $key) {
foreach($this->lines as $line) {
if($line['type'] != 'entry') continue;
if($line['section'] != $section) continue;
if($line['key'] != $key) continue;
return $line['value'];
}
throw new Exception('Missing Section or Key');
}
public function set($section, $key, $value) {
foreach($this->lines as &$line) {
if($line['type'] != 'entry') continue;
if($line['section'] != $section) continue;
if($line['key'] != $key) continue;
$line['value'] = $value;
$line['data'] = $key . " = " . $value . "\r\n";
return;
}
throw new Exception('Missing Section or Key');
}
public function write($file) {
$fp = fopen($file, 'w');
foreach($this->lines as $line) {
fwrite($fp, $line['data']);
}
fclose($fp);
}
}
$ini = new ini();
$ini->read("C:\\php.ini");
$ini->set('PHP', 'engine', 'Off');
echo $ini->get('PHP', 'engine');
$ini->write("C:\\php.ini");
क्या आपके पास कुछ नमूना इनपुट और आउटपुट है? –
यह एक मानक आईएनआई फ़ाइल की तरह दिखता है, जैसे php.ini, 4-5 सेक्शन जिनमें कुछ महत्वपूर्ण = मूल्य प्रविष्टियां हैं, उनके अंदर – Alex
इस तरह कुछ संदेह है (PHP के लिए)। मेरे पास एक ['editconfigini.php'] होगा (http://apt.include-once.org/php/), लेकिन यह खंडों को अनदेखा करता है और केवल एक बाध्य कुंजी = मान प्रारूप की परवाह करता है। – mario