note 61121 deleted from function.parse-ini-file by danbrown
| From: | danbrown@php.net | Date: | Sun, 23 Jan 2011 17:13:17 +0000 |
| Subject: | note 61121 deleted from function.parse-ini-file by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176164@lists.php.net to get a copy of this message | ||
Note Submitter: ahull at clydemarine dot com
----
I had a look at the code for function parse_ini_file_quotes_safe(
and added in the ability to preserve comments.
<?php
// Parse a file into an array following the rules for ini files as follows
//
// Looks for [] characters to mark section headings and = chars to mark the break between the key
and its values.
// Also keeps comments delimited by any of the characters in $comments_chars in the array numbered
as they are found.
//
// Note writing back the array will necessarily move the comments to the beginning of the section,
// even if they are found within
// a section simply because there is no exact place-holder information stored in the array.
// This could of course be a problem.
// Also the Write array routine will have to be modified
// to correctly write back comments otherwise they will appear as blank sections called [comment{x}]
function parse_ini_file_quotes_safe($f)
{
$newline = "<br>";
$null = "";
$r=$null;
$first_char = "";
$sec=$null;
$comment_chars="/*<;#?>";
$num_comments = "0";
$header_section = "";
//Read to end of file with the newlines still attached into $f
$f=@file($f);
// Process all lines from 0 to count($f)
for ($i=0;$i<@count($f);$i++)
{
$newsec=0;
$w=@trim($f[$i]);
$first_char = @substr($w,0,1);
if ($w)
{
if ((!$r) or ($sec))
{
// Look for [] chars round section headings
if ((@substr($w,0,1)=="[") and (@substr($w,-1,1))=="]")
{$sec=@substr($w,1,@strlen($w)-2);$newsec=1;}
// Look for comments and number into array
if ((stristr($comment_chars, $first_char) === FALSE)) {} else
{$sec=$w;$k="Comment".$num_comments;$num_comments = $num_comments
+1;$v=$w;$newsec=1;$r[$k]=$v;echo "comment".$w.$newline;}
//
}
if (!$newsec)
{
//
// Look for the = char to allow us to split the section into key and value
$w=@explode("=",$w);$k=@trim($w[0]);unset($w[0]); $v=@trim(@implode("=",$w));
// look for the new lines
if ((@substr($v,0,1)=="\"") and (@substr($v,-1,1)=="\""))
{$v=@substr($v,1,@strlen($v)-2);}
if ($sec) {$r[$sec][$k]=$v;} else {$r[$k]=$v;}
}
}
}
return $r;
}
?>