Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7622041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:21:08+00:00 2026-05-31T04:21:08+00:00

I need to be able to edit .ini files (which I’m reading with parse_ini_file),

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:21:09+00:00Added an answer on May 31, 2026 at 4:21 am

    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");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I need to be able to edit the file comments in .rar files
I need to be able to edit messages in my error queue (so that
(Summary: My users need to be able to edit the structure of their dynamically
I need to be able edit the content of index.dat file programmatically (C:\Documents and
In my MVC 3 application i need to be able to edit text with
My application collects data about the battery. I need to be able to edit
I would like to be able to edit conflicts using DiffMerge but when I
I need to be able to edit vb.net code using visual studio without stopping
I need to be able to edit a table of data in the browser.
I need to be able to edit nodes from a client outside of Drupal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.