quick way to parse a file?

This is a discussion on quick way to parse a file? within the PHP Language forums, part of the PHP Programming Forums category; Hi, I have a basic file, call it "config.php" <?php $db_hostname = "localhost"; $db_socket = "/...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-07-2006
laredotornado@zipmail.com
 
Posts: n/a
Default quick way to parse a file?

Hi,

I have a basic file, call it "config.php"

<?php

$db_hostname = "localhost";
$db_socket = "/tmp/mysql5.sock";
$mysql_host = $db_hostname;
if (!empty($db_socket))
$mysql_host .= ":$db_socket";
$db_name = "DB1";
$db_user = "username";
$db_password = "password";

// Connect and select the appropriate db.
mysql_connect($mysql_host, $db_user, $db_password) or die("Can't
connect to MySQL: '" . mysql_error() . "'");
mysql_select_db($db_name) or die("Failed to select db: '" .
mysql_error() . "'");

?>

What is the shortest way to parse this file and extract the value "DB1"
(wihtout the quotes). THat value is guaranteed to be in a line of the
form '$db_name = "NAME"' but that line won't necessarily be the 8th
line of the file.

I'm using PHP 4.4.4. Thanks, - Dave

Reply With Quote
  #2 (permalink)  
Old 12-07-2006
NC
 
Posts: n/a
Default Re: quick way to parse a file?

laredotornado@zipmail.com wrote:
>
> I have a basic file, call it "config.php"
>
> <?php
>
> $db_hostname = "localhost";
> $db_socket = "/tmp/mysql5.sock";
> $mysql_host = $db_hostname;
> if (!empty($db_socket))
> $mysql_host .= ":$db_socket";
> $db_name = "DB1";
> $db_user = "username";
> $db_password = "password";
>
> // Connect and select the appropriate db.
> mysql_connect($mysql_host, $db_user, $db_password) or die("Can't
> connect to MySQL: '" . mysql_error() . "'");
> mysql_select_db($db_name) or die("Failed to select db: '" .
> mysql_error() . "'");
>
> ?>
>
> What is the shortest way to parse this file and extract the value
> "DB1" (wihtout the quotes).


Get rid of connection code (or wrap it into a function) and include()
the file.

Cheers,
NC

Reply With Quote
Reply


Thread Tools
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

vB 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 12:13 AM.


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