Bluehost.com Web Hosting $6.95

there has to be a better way...

This is a discussion on there has to be a better way... within the PHP General forums, part of the PHP Programming Forums category; I need to read (write comes later) from a config file that we used to handle manually. I'm getting ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-22-2003
Jswalter
 
Posts: n/a
Default there has to be a better way...

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
- 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
- kill original array element

There must be a better way to do this.

All this seems a bit over kill to me.

Does anyone have any ideas on this?

Thanks

Walter

This is what I have...

<?php

$config = $list . '/home/walter/vmd/config';

// Open the file
$handle = fopen ($config, "r");

// Read entire file into var
$content = fread($handle, filesize($config));

// convert var into array and explode file via line break
$content = split("\r\n", $content);

// close file
fclose($handle);

// Loop through file contents array
foreach ($content as $i => $value)
{
// If we have any data in this line
if (! empty ($value))
{
// If this line is not a comment
if ( $value{0} != '#')
{
list($a, $b) = split("=", $value);
$content[$a] = $b;
}

// kill original array element
unset($content[$i]);
}
}

// show me what I have
echo '<pre>';
echo print_r($content);
echo '</pre>';

?>

# Sample config file data, just 2 lines from the file...
#
# The "Personal Name" of the list, used in outgoing headers.
# If empty, default is the same as the list's username.
# if explicitly `false', then it is redefined empty.
LIST_NAME="RMT Working Group"

# The address of the list's admin or owner.
# if explicitly `false', then it is redefined empty.
ADMIN_ADDRESS=walter@torres.ws

....
Reply With Quote
  #2 (permalink)  
Old 10-22-2003
- Edwin -
 
Posts: n/a
Default Re: [PHP] there has to be a better way...

On Wed, 22 Oct 2003 03:10:44 -0500
"jsWalter" <jsWalter@torres.ws> wrote:

[snip]
> There must be a better way to do this.

[/snip]

http://www.php.net/manual/en/functio...e-ini-file.php ?

- E -
__________________________________________________
Do You Yahoo!?
Yahoo! BB is Broadband by Yahoo!
http://bb.yahoo.co.jp/
Reply With Quote
  #3 (permalink)  
Old 10-22-2003
Nick Jordan
 
Posts: n/a
Default Re: [PHP] there has to be a better way...

"jsWalter" <jsWalter@torres.ws> wrote on 22/10/2003 09:10:44:

> I need to read (write comes later) from a config file that we used to

handle
> manually.


> There must be a better way to do this.


Check out the parse_ini_file() function.

Nick
Reply With Quote
  #4 (permalink)  
Old 10-22-2003
Daevid Vincent
 
Posts: n/a
Default RE: [PHP] there has to be a better way...

Here is a snippet from my dhcp web page found on my site below... Not
exactly what you want, but could help you start...

// read in the dhcp_map.ini file to map MAC addresses to images
$mapFile = "./dhcp_map.ini";
if( $fp = @fopen($mapFile, "r") )
{
while( $line = fgets($fp, 1024) )
if ($line{0} != "#") $tempMap .= $line; // strip
out # comments
} else echo "ERROR: Can't read ".$mapFile."<BR>";
fclose ($fp);
$map = explode("\n", $tempMap);
//print_r($map);
for($i = 0; $i < count($map); $i++)
{
list($mac, $image, $name) = preg_split("/\s/",$map[$i], -1,
PREG_SPLIT_NO_EMPTY);
//if ( array_key_exists($mac, $machine))
$machine[$mac]->setImage($image);
if ( isset($machine[$mac]) )
$machine[$mac]->setImage($image);
if ( isset($machine[$mac]) && $name != "" &&
$machine[$mac]->getName() == "") $machine[$mac]->name = $name;
}
unset($tempMap);
unset($map);


# "dhcp_map.ini" file
# MAC ADDRESS: GIF IMAGE: NAME:
00:09:12:86:48:54 linux LINUX
00:0B:AD:31:AA:A6 tivo Thad's
00:0B:AD:08:38:C3 tivo Daevid's



Daevid Vincent
http://daevid.com


> -----Original Message-----
> From: jsWalter [mailto:jsWalter@torres.ws]
> Sent: Wednesday, October 22, 2003 1:11 AM
> To: php-general@lists.php.net
> Subject: [php] there has to be a better way...
>
> 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
> - 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
> - kill original array element
>
> There must be a better way to do this.
>
> All this seems a bit over kill to me.
>
> Does anyone have any ideas on this?
>
> Thanks
>
> Walter
>
> This is what I have...
>
> <?php
>
> $config = $list . '/home/walter/vmd/config';
>
> // Open the file
> $handle = fopen ($config, "r");
>
> // Read entire file into var
> $content = fread($handle, filesize($config));
>
> // convert var into array and explode file via line break
> $content = split("\r\n", $content);
>
> // close file
> fclose($handle);
>
> // Loop through file contents array
> foreach ($content as $i => $value)
> {
> // If we have any data in this line
> if (! empty ($value))
> {
> // If this line is not a comment
> if ( $value{0} != '#')
> {
> list($a, $b) = split("=", $value);
> $content[$a] = $b;
> }
>
> // kill original array element
> unset($content[$i]);
> }
> }
>
> // show me what I have
> echo '<pre>';
> echo print_r($content);
> echo '</pre>';
>
> ?>
>
> # Sample config file data, just 2 lines from the file...
> #
> # The "Personal Name" of the list, used in outgoing headers.
> # If empty, default is the same as the list's username.
> # if explicitly `false', then it is redefined empty.
> LIST_NAME="RMT Working Group"
>
> # The address of the list's admin or owner.
> # if explicitly `false', then it is redefined empty.
> ADMIN_ADDRESS=walter@torres.ws
>
> ...
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Reply With Quote
  #5 (permalink)  
Old 10-23-2003
Jswalter
 
Posts: n/a
Default Re: [PHP] there has to be a better way...

"- Edwin -" <copperwa11s@yahoo.co.jp> wrote in message
news:20031022172105.1ea9af7c.copperwa11s@yahoo.co. jp...
> On Wed, 22 Oct 2003 03:10:44 -0500
> "jsWalter" <jsWalter@torres.ws> wrote:
>
> [snip]
> > There must be a better way to do this.

> [/snip]
>
> http://www.php.net/manual/en/functio...e-ini-file.php ?


thanks.

But, my concern with that is my config files use '#' for comment lines, not
';'

Is there a way around this?

Walter



Reply With Quote
  #6 (permalink)  
Old 10-23-2003
Jswalter
 
Posts: n/a
Default Re: [PHP] there has to be a better way...


"Daevid Vincent" <daevid@daevid.com> wrote in message
news:200310221924.h9MJOAq5018186@daevid...
> Here is a snippet from my dhcp web page found on my site below... Not
> exactly what you want, but could help you start...


thanks!


> // read in the dhcp_map.ini file to map MAC addresses to images
> $mapFile = "./dhcp_map.ini";
> if( $fp = @fopen($mapFile, "r") )
> {
> while( $line = fgets($fp, 1024) )


this pulls out 1024 bytes of data form the file. right?

or does this pull UPTO 1024 bytes of data or the EOL, which ever is first?


> if ($line{0} != "#") $tempMap .= $line; // strip
> out # comments
> } else echo "ERROR: Can't read ".$mapFile."<BR>";
> fclose ($fp);
> $map = explode("\n", $tempMap);


OK. $tempMap is a string var. Each line of the file has a /n delimiting it.

Now make an array of these line.

Right?

> for($i = 0; $i < count($map); $i++)
> {
> list($mac, $image, $name) = preg_split("/\s/",$map[$i], -1,
> PREG_SPLIT_NO_EMPTY);


walk down the map array.

split each element on the SPACE, but don't deal with blank lines

....

seems almost the same.

nice use of PREG_SPLIT. I guess that helps with the BLANK lines.

if fgets() onlt pulls out so much data, not to EOL, why use it?

If I understand right, if I pull out 1028 of data, the '#' on each line
could be anywhere in that buffer, not just at the first character.

That's why I pulled in the entire file into a string var and then exploded
it into an array split on EOL characters.

Am I missing something?

Thanks for your sample.

Walter
Reply With Quote
  #7 (permalink)  
Old 10-23-2003
John W. Holmes
 
Posts: n/a
Default Re: [PHP] there has to be a better way...

jsWalter wrote:

> 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.


Don't know if someone said this or not, but why not just use
parse_ini_file() ?

--
---John Holmes...

Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/

php|architect: The Magazine for PHP Professionals – www.phparch.com
Reply With Quote
  #8 (permalink)  
Old 10-23-2003
Curt Zirzow
 
Posts: n/a
Default Re: [PHP] there has to be a better way...

* Thus wrote jsWalter (jsWalter@torres.ws):
> 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/te...y/parseini.php

HTH,

Cheers

Curt
--
"My PHP key is worn out"

PHP List stats since 1997:
http://zirzow.dyndns.org/html/mlists/
Reply With Quote
  #9 (permalink)  
Old 10-23-2003
Colin Kettenacker
 
Posts: n/a
Default Re: [PHP] there has to be a better way...

Hi Walter,

You may want to look into PEAR's config package. It does pretty much all you
have listed here and a lot more. I just started looking into it today. I
haven't looked closely at the code so I don't know how efficiently it
handles everything it does but it may give you some ideas. As far as I tell
it can handle Generic Config files, .ini files, XML config files and more.

http://pear.php.net/package/Config
http://pear.php.net/manual/en/package.configuration.php

ck
--
Cheap Domain Registration | Web Hosting | Email Packages | + more
Fantastic prices -- Even better service.
http://www.hosttohost.net


jsWalter jsWalter@torres.ws on 10/22/03 1:10 AM wrote:

> 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
> - 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
> - kill original array element
>
> There must be a better way to do this.
>
> All this seems a bit over kill to me.
>
> Does anyone have any ideas on this?
>
> Thanks
>
> Walter
>
> This is what I have...
>
> <?php
>
> $config = $list . '/home/walter/vmd/config';
>
> // Open the file
> $handle = fopen ($config, "r");
>
> // Read entire file into var
> $content = fread($handle, filesize($config));
>
> // convert var into array and explode file via line break
> $content = split("\r\n", $content);
>
> // close file
> fclose($handle);
>
> // Loop through file contents array
> foreach ($content as $i => $value)
> {
> // If we have any data in this line
> if (! empty ($value))
> {
> // If this line is not a comment
> if ( $value{0} != '#')
> {
> list($a, $b) = split("=", $value);
> $content[$a] = $b;
> }
>
> // kill original array element
> unset($content[$i]);
> }
> }
>
> // show me what I have
> echo '<pre>';
> echo print_r($content);
> echo '</pre>';
>
> ?>
>
> # Sample config file data, just 2 lines from the file...
> #
> # The "Personal Name" of the list, used in outgoing headers.
> # If empty, default is the same as the list's username.
> # if explicitly `false', then it is redefined empty.
> LIST_NAME="RMT Working Group"
>
> # The address of the list's admin or owner.
> # if explicitly `false', then it is redefined empty.
> ADMIN_ADDRESS=walter@torres.ws
>
> ...

Reply With Quote
  #10 (permalink)  
Old 10-23-2003
Walter Torres
 
Posts: n/a
Default RE: [PHP] there has to be a better way...

> -----Original Message-----
> From: Colin Kettenacker [mailto:list@novus-tele.net]
> Sent: Thursday, October 23, 2003 12:53 AM
> To: jsWalter; php-general@lists.php.net
> Subject: Re: [php] there has to be a better way...
>
>
> Hi Walter,
>
> You may want to look into PEAR's config package. It does pretty
> much all you
> have listed here and a lot more. I just started looking into it today.


Thanks Colin.

I saw that it, read it, played with it.

It was just way to big a clunky for what I was looking to do.

I have what "need" to make this work.

I was just hoping to have a better method of reading the file and ignoring
the blank lines and the commented lines.

I just have this thing about double level IF statements.

Thanks.

Walter
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:45 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0