解析ini文件时出现php字符串比较错误

解析ini文件时出现php字符串比较错误,php,parsing,string-comparison,ini,Php,Parsing,String Comparison,Ini,我有一个简单的ini文件: [section_one] test = abc [section_two] yada = blah #and_so=on 我编写了一个解析器函数来更新它b/c我的注释字符是“#”而不是“;”--所以parse_ini_file()抱怨。但以下是我的快速而肮脏的解决方案: <?php function edit_ini_file ($fName, $fKey, $fVal) { print"<h4>Search: $fKey = $fVal

我有一个简单的ini文件:

[section_one]
test = abc

[section_two]
yada = blah
#and_so=on
我编写了一个解析器函数来更新它b/c我的注释字符是“#”而不是“;”--所以parse_ini_file()抱怨。但以下是我的快速而肮脏的解决方案:

<?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
那个小家伙快把我逼疯了我知道一定是什么蠢事

如果方向正确,我们将不胜感激……

是$fKey还是$fKey

做个决定

)()

你知道,对吧?
if (trim($curKey) === trim($fkey)) { doSomething.. }