* Thus wrote jsWalter ([EMAIL PROTECTED]): > I need to read (write comes later) from a config file that we used to handle > manually. > > I'm getting lazy, so I'm writing a web interface for this. > > What I have does this... > - open a given file > - dump entire file into a string > - explode string into an array at the EOL marker > - walk down this new array
Instead of doing these last three, just walk through each line of the file. These tasks are just adding extra overhead. > - decide if there is anything in current element > - decide if current line is a comment > - split line at '=' into 2 variables > - add new key and value from these variables back into array We can kill these four conditions in two lines of code. (see below) > - kill original array element Dont need this if we are reading directly from the file. > > There must be a better way to do this. Pehraps something like this: <?php $fp = fopen('test.ini', 'r'); while (! feof($fp) ) { $line = fgets($fp, 1024); /* dont need this trim, only used for the print line */ $line = trim($line); print "[$line]\n"; if (preg_match('/^ (?<!\#) (?#>No # in the beginning, not needed but makes pcre skip it, much faster if line is a comment) (\w+) (?# The key must be a word ) \s*=\s* (?# Allow space on both sides of =) (?(?=") (?# If quoted ) "([^"]*)" (?# Grab all but quoted) | (?# else ) (\w+) (?# grab the word ) ) /x', $line, $matches) ) { //print_r($matches); $content[$matches[1]] = ($matches[2]? $matches[2]: $matches[3]); } } print_r($content); ?> Ok, i lied it was more than two lines, but I made the regular expression readable (if you can call it that :) A demo of this can be seen here: http://zirzow.dyndns.org/html/php/tests/array/parseini.php HTH, Cheers Curt -- "My PHP key is worn out" PHP List stats since 1997: http://zirzow.dyndns.org/html/mlists/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php