0

I have a simple ini file:

[section_one]
test = abc

[section_two]
yada = blah
#and_so=on

I wrote a parser function to update it b/c my comment char is '#' instead of ';' -- so parse_ini_file() complains. But here's my quick & dirty solution:

<?php
function edit_ini_file ($fName, $fKey, $fVal) {
  print"<h4>Search: $fKey = $fVal </h4>";
  // declarations
  $_comntChar='#';
  $_headrChar='[';
  $keyMatch=FALSE;
  $iniArray = array(); // new array in memory
  $dataOut = ''; // datastream for output file 
  // temp cursor vars for looping & reporting
  $verbose = 1;
  $curSec = ''; // current section
  $curKey = ''; // current key
  $curVal = ''; // current value 
  $curLine=-1;  // current line Number 
  if (isset($fName)) {
    if (!is_file($fName)) return FALSE;
    $lines = file($fName);
    //read file as array of lines
    foreach ($lines as $line) {
        $curLine+=1;
        if ($verbose) print '<br/>['.$curLine.'][IN:] '.$line; 
      //parse for k/v pairs, comments & section headings
      if (   (strpos($line,$_headrChar)==1) // assume heading
          || (strpos($line,$_comntChar)==1) // assume comment
          || (!strpos($line,'=')) // also skip invalid k/v pairs  
         ){
            array_push($iniArray, $lines[$curLine] ); //stuff the entire line into array.
            if ($verbose) print " - no k/v";
       } else { // assume valid k/v pair
        //split k/v pairs & parse for match
        $pair = explode('=', $line);
        $curKey = trim($pair[0]);
        $curVal = trim($pair[1]);
        if ($verbose) print "[KV]: k=$curKey:v=$curVal";
        if (trim($curKey) === trim($fkey)) { // <=== THE BUGGER: never returns true: 
          $keyMatch=TRUE;
          print ("MATCH: Replacing value in for key=$curKey in Section $curSec at line $curLine<br/>");
          array_push ($iniArray, array($curKey => $fVal ));
         } else {
         array_push ($iniArray, array($curKey => $curVal ));    
        } //end-matcher
       } //end-parser
     } //end foreach   
    if (!$keyMatch) { //append new data to end
      print "<br/>Key not Found. Appending! <br/>";
      array_push ($iniArray, array($fKey => $fVal) );
     } 
  //reformat nested array as one long string for a single bulk-write to disk.
  foreach($iniArray as $curSect => $val) {
    if (is_array($val)) {
        foreach($val as $curKey => $curVal) 
             $dataOut .= "$curKey = $curVal\n"; 
    } else { $dataOut .= "$val"; }   
  }
  print "dataout:<pre>" .$dataOut. "</pre>";
  //put file & pass return val
  return (file_put_contents($filename, $dataOut)) ? TRUE : FALSE;    
 }//if isset
}//end-func

Basically I'm just exploding a text file line-by-line stuffing a new array and dumping it back to the disk

MY BUG: for some reason my comparison trying strcmp() or "==" or "===" never seems to work...

if (trim($curKey) === trim($fkey)) { doSomething.. }

That little BUGGER is driving me nuts b/c I know it's gotta be something stupid.

ANy point in the right direction would be appreciated...

1
  • 1
    You do know about parse_ini_file(), right? Commented Sep 1, 2011 at 2:26

1 Answer 1

1

Is it $fKey or $fkey?

Make a decision!

;)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.