note 49141 added to function.parse-ini-file

From: Date: Tue, 18 Jan 2005 00:21:02 +0000
Subject: note 49141 added to function.parse-ini-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-83529@lists.php.net to get a copy of this message
As a Java programmer, I find PHPs lack of handing of multi-line ".properties" files a bit of a pain. I didn't see PEAR::Config handle this, so I hacked together a quick Properties class. This is by no means complete. It works for me, but I'm sure that someone can improve it. I'm also not an expert in PHP, so it may look like a kludge. Anyway, here it is: <?php /* * Properties class. Similar to Java Properties, deals with multi-line * properties files. * * Created on Jan 17, 2005 * * @author Michael V. Scovetta * This code is released under the GPL license. */ class Properties { var $properties; var $keyValueSeparators = "=: \t\r\n"; var $whiteSpaceChars = " \t\r\n"; function Properties($file = null) { $this->properties = array(); if ($file) { $this->load($file); } } function set_property( $key, $value ) { $this->properties[$key] = $value; } function get_property( $key ) { return $this->properties[$key]; } function load( $file ) { $lines = file($file); $lc = 0; $cont = false; foreach ($lines as $line) { if (!$cont) { $line = ltrim($line, $this->whiteSpaceChars); $key = $this->findFirstIn($line, $this->keyValueSeparators); if ($key === false) continue; $value = substr($line, $key+2); $value = trim($value, $this->whiteSpaceChars); $key = substr($line, 0, $key+1); $key = trim($key, $this->whiteSpaceChars); if (substr($value, strlen($value)-1, 1) === '\\') { $value = substr($value, 0, strlen($value)-1); $cont = true; } else { $this->properties[$key] = $value; } } else { $line = trim($line, $this->whiteSpaceChars); if (substr($line, strlen($line)-1, 1) === '\\') { $value .= substr($line, 0, strlen($line)-1); } else { $cont = false; $value .= $line; $this->properties[$key] = $value; } } } } function continueLine($line) { $slashCount = 0; $index = strlen($line) - 1; while (($index >= 0) && (substr($line, $index--, 1) == '\\')) $slashCount++; return ($slashCount % 2 == 1); } /** * Finds the first occurance of any character of $choices in $txt */ function findFirstIn( $txt, $choices, $start = null) { $pos = -1; $arr = array(); for ($i=0; $i<strlen($choices); $i++) { array_push($arr, substr($choices, $i, 1)); } foreach( $arr as $v ) { $p = strpos( $txt, $v, $start ); if ($p===FALSE) continue; if (($p<$pos)||($pos==-1)) $pos = $p; } return $pos; } function toArray() { return $this->properties; } } ?> ---- Manual Page -- http://www.php.net/manual/en/function.parse-ini-file.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+49141 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49141&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49141&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49141&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49141&report=yes&reason=useless+example Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49141&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+49141&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#83529) next »