473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

Dec 7 '06 #1
1 1222
NC
la***********@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

Dec 7 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Hank | last post by:
Hi, I have a CSV file from excel that looks like this (simplified): name,description,type1,type2,name,filename test1,this is a test,0.000,1.000,, test2,another...
3
by: winderjj | last post by:
Hi All, I need everyones opinion. I am very new to XML but am temporarily putting all my efforts into using it. This is what I need to do. Write an xml parser (in C) that will parse a...
2
by: Horny Porno-thologist | last post by:
Dear everyone, I managed to get most of my code (nesting documents etc.) worked out, but there are a few niggles. The first one is as follows: My first document is: <?xml version="1.0"...
5
by: Ryan Smith | last post by:
I have a code behind VB app that I have developed and am trying to place in a subfolder of a website that I have. I run the app on my dev box fine with no errors but when I upload it to the...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
5
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
11
by: Peter Pei | last post by:
One bad design about elementtree is that it has different ways parsing a string and a file, even worse they return different objects: 1) When you parse a file, you can simply call parse, which...
5
by: goldtech | last post by:
SAX XML Parse Python error message Hi, My first attempt at SAX, but have an error message I need help with. I cite the error message, code, and xml below. Be grateful if anyone can tell me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.