All pastes #1889249 Raw Edit

Write/Read INI in PHP

public php v1 · immutable
#1889249 ·published 2010-06-23 14:42 UTC
rendered paste body
<?function readini($file,$title,$item) {  $source = file($file);  $t_match = "/^\[".$title."\]/i";  $s_title = "no";  foreach ($source as $temp) {    if (preg_match("/^\[".$title."\]/i",$temp)) {      $s_title = "yes";    } elseif (preg_match("/^\[.+\]/",$temp) && $s_title = "yes") {	  $s_title = "no";	} elseif (preg_match("/^".$item."[ ]{0,}=/i",$temp) && $s_title == "yes") {	  $result = explode("=",$temp);	  return trim($result[1]);	}  }}function writeini($file,$title,$item,$data) {  $source = file($file);  $t_match = "/^\[".$title."\]/i";  $s_title = "";  $c = "0";  $o = "";  foreach ($source as $temp) {    $c++;    if (preg_match("/^\[.+\]/i",$temp)) {	  if (preg_match($t_match,$l_title)) {	    $done = "yes";	    $f_write .= $item."=".$data."\n";	  }	  $l_title = $temp;	  $f_write .= $temp;	} elseif (preg_match("/^".$item."=/i",$temp) && preg_match($t_match,$l_title)) {	  $done = "yes";	  $f_write .= $item."=".$data."\n";	  $l_title = "";	} elseif ($c == count($source) && $done != "yes") {	  if (preg_match($t_match,$l_title)) {	    $f_write .= $temp.$item."=".$data."\n";	  } else {		$f_write .= $temp."[".$title."]\n".$item."=".$data."\n";	  }	} else {	  $f_write .= $temp;	}  }  $ini_write = fopen($file,'w');  fwrite($ini_write,$f_write);  fclose($ini_write);}?>