Ok,
I have tried, and googled and this is a last resort.
I have a config file like so: -
//comment
-
variable = "value" //for text
-
variable = value //numeric
-
I have managed to get php to output an array like so (using the explode function) -
Array
-
(
-
[0] => variable = "value"
-
)
-
-
and not ...
-
-
Array
-
(
-
[ variable] => "value"
-
)
How do I do this?
Many Thanks!
15 3162
[PHP]//Declare variables
$t_variable = "value" //for text
$v_variable = 1 //numeric[/PHP]Populate the array
[PHP]$arry = array();
$arry[$t_variable] = $v_variable;[/PHP]or you can try
[PHP]$arry = array($t_variable=>$v_variable);[/PHP]
Ah, but the config is in a file called config.cfg
Ah, but the config is in a file called config.cfg
You lost me there! Could you please rephrase the problem? As I understand it now you have a file and you want to move its content into an array(?).
Ronald
Ok,
I have a file called server.cfg which contains:
//comments
servername = "myserver"
port = 1234
An then I want my PHP to break that down into variables (I have been using Arrays there maybe a better way).
$servername =
So that I can create a load of input boxes from the config to make it easy to edit.
Thanks!
Putting it in an array (here done with the split function) can be done as follows, assuming that you have a 'clean' input line, e.g. comments and the likes taken out
[php]
list($key,$val) = split('=', $inputline);
$array[$key]=$val;
[/php]
Ronald
Take a look at parse_ini_file().
You are so right! In my drive to populate an array via code I completely overlooked the most obvious solution. Thanks.
Ronald
Atli 5,058
Expert 4TB
I've changed the title of this thread to better describe it's topic.
Using good, descriptive titles that follow the Posting Guidelines will increase your chances of getting you questions answered! Moderator
Yeh, I tried "parse_ini_file()" but it was a bit crasy.
-
<?
-
-
$url = ('server.cfg');
-
-
$fh = fopen($url, 'r');
-
$file_cfg = fread($fh,10000);
-
fclose($fh);
-
-
list($key,$val) = split(' ', $file_cfg);
-
$array[$key]=$val;
-
-
print_r($array['servername']
-
-
?>
-
That only gets the first line.
How Do I get all of them?
BTW I got rid of the '=' the values are split by an ' '
Do the file reading in a loop. Like this:
[php] $fh = @fopen($url, "r");
while (!feof($fh)) {
$file_cfg = fgets($fh);
list($key,$val) = split(' ', $file_cfg);
$array[$key]=$val;
}[/php]
Ronald
Heya, Hugo.
Please use CODE tags when posting source code:
[CODE=php]
PHP code goes here.
[/CODE]
parse_ini_file() looks like it will do exactly what you're looking for. Pay special attention to the process_sections parameter.
Do the file reading in a loop. Like this:
[php] $fh = @fopen($url, "r");
while (!feof($fh)) {
$file_cfg = fgets($fh);
list($key,$val) = split(' ', $file_cfg);
$array[$key]=$val;
}[/php]
Ronald
W00t thanks your pro :p
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Charlie Smith |
last post by:
Hello All
I'm writing a little app for my personal use and want to store the
configuration data in an xml file. I found PEAR::Config that lets
me read the config data into my app. But so far,...
|
by: iop |
last post by:
Hello there,
I'd like to "parse" an entire multi-dimension array like this :
APP
APP
without knowing "framework" or "config" or anything passed as variables...
'cause it's simple to call...
|
by: |
last post by:
Hi,
I need to read a big CSV file, where different fields should be converted to
different types,
such as int, double, datetime, SqlMoney, etc.
I have an array, which describes the fields and...
|
by: William Stacey [MVP] |
last post by:
Anyone know of some library that will parse files like following:
options {
directory "/etc";
allow-query { any; }; // This is the default
recursion no;
listen-on { 192.168.0.225;...
|
by: aabdis |
last post by:
Hello All....
I have a question... i have two Windows apps (one UI, and one service)
which use a common DLL that hands out database results and such to the
two apps. The DLL always connects to...
|
by: Tonino Greco |
last post by:
Hi all,
I wonder if someone on the list can offer a suggestion as to how I can
parse a config file that has the form:
group1
name1
name2
group2
|
by: Bob Bedford |
last post by:
Hi all,
I need help for parsing a huge XML file with some ISP limits (I can't change
ISP, so this isn't a solution).
The ISP limits the scripts to 16MB and 10 seconds. Those params can't be...
|
by: deerhide |
last post by:
Hi, I was updating my website, well trying to... and I somehow messed it up. I didnt build it, I bought it so I don't know alot about programming. I receive these errors when going to...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
| |