note 72838 added to function.parse-ini-file

From: Date: Fri, 02 Feb 2007 18:22:39 +0000
Subject: note 72838 added to function.parse-ini-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-122571@lists.php.net to get a copy of this message
I modified phpcoder's readINIFile function to allow multi-lined values. Adding a backslash (\) to the end of a line indicates that the whole of the next line should be appended to the value. Leading whitespace is ignored on continues lines, whitespace before the backslash is preserved. This is the same as the Java Properties spec: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html <?php function readINIfile ($filename, $commentchar) { $array1 = file($filename); $section = ''; for ($line_num = 0; $line_num <= sizeof($array1); $line_num++) { $filedata = $array1[$line_num]; $dataline = trim($filedata); $firstchar = substr($dataline, 0, 1); if ($firstchar!=$commentchar && $dataline!='') { //It's an entry (not a comment and not a blank line) if ($firstchar == '[' && substr($dataline, -1, 1) == ']') { //It's a section $section = strtolower(substr($dataline, 1, -1)); }else{ //It's a key... $delimiter = strpos($dataline, '='); if ($delimiter > 0) { //...with a value $key = strtolower(trim(substr($dataline, 0, $delimiter))); $array2[$section][$key] = ''; $value = trim(substr($dataline, $delimiter + 1)); while (substr($value, -1, 1) == '\\') { //...value continues on the next line $value = substr($value, 0, strlen($value)-1); $array2[$section][$key] .= stripcslashes($value); $line_num++; $value = trim($array1[$line_num]); } $array2[$section][$key] .= stripcslashes($value); $array2[$section][$key] = trim($array2[$section][$key]); if (substr($array2[$section][$key], 0, 1) == '"' && substr($array2[$section][$key], -1, 1) == '"') { $array2[$section][$key] = substr($array2[$section][$key], 1, -1); } }else{ //...without a value $array2[$section][strtolower(trim($dataline))]=''; } } }else{ //It's a comment or blank line. Ignore. } } return $array2; } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 208.205.82.65 (proxied: 1.1 L17FPROX1) ---- Manual Page -- http://www.php.net/manual/en/function.parse-ini-file.php Edit -- https://master.php.net/note/edit/72838 Del: integrated -- https://master.php.net/note/delete/72838/integrated Del: useless -- https://master.php.net/note/delete/72838/useless Del: bad code -- https://master.php.net/note/delete/72838/bad+code Del: spam -- https://master.php.net/note/delete/72838/spam Del: non-english -- https://master.php.net/note/delete/72838/non-english Del: in docs -- https://master.php.net/note/delete/72838/in+docs Del: other reasons-- https://master.php.net/note/delete/72838 Reject -- https://master.php.net/note/reject/72838 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#122571) next »