note 61121 added to function.parse-ini-file

From: Date: Thu, 26 Jan 2006 09:33:47 +0000
Subject: note 61121 added to function.parse-ini-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-102466@lists.php.net to get a copy of this message
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; } ?> ---- Server IP: 84.18.202.180 Probable Submitter: 82.109.197.226 ---- Manual Page -- http://www.php.net/manual/en/function.parse-ini-file.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+61121 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61121&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61121&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61121&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61121&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61121&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61121&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61121&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61121&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#102466) next »