Home » Php » parsing – Good PHP classes that manipulate .ini files

parsing – Good PHP classes that manipulate .ini files

Posted by: admin July 12, 2020 Leave a comment

Questions:

I need to be able to edit .ini files (which I’m reading with parse_ini_file), but in such a way that comments and formatting (line breaks, indenting) is preserved.

Do you know any good classes that have nice and optimized functions for this kind of stuff?

How to&Answers:

You could try starting from this, it reads the ini file, and preserves the settings on write, you would have to extend it to support adding new entries:

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");

Answer:

I’ve not used the config writer in the Zend component framework, but I’ve used the config reader, and it was very reliable. Would certainly be worth a go.

Answer:

Here’s a few you might want to try out:

http://www.phpclasses.org/package/3089-PHP-Manipulate-configuration-files-in-the-ini-format.html

http://www.phpclasses.org/package/6545-PHP-Read-and-write-configuration-values-to-INI-files.html

Answer:

The PEAR::Config package has support for comments, so I assume it does preserve them. Probably it is fitting your needs.