quick way to parse a file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    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_soc ket))
    $mysql_host .= ":$db_socke t";
    $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

  • NC

    #2
    Re: quick way to parse a file?

    laredotornado@z ipmail.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_soc ket))
    $mysql_host .= ":$db_socke t";
    $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

    Comment

    Working...