parse_ini_file(), not accepting ampersands (&)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
303tech
Forum Commoner
Posts: 31
Joined: Sun Mar 11, 2007 3:25 pm

parse_ini_file(), not accepting ampersands (&)

Post by 303tech »

I asked about this in another post but I must not have been clear enough with my problem.

if there is an ampersand in the ini file which i parse, the result =0
The result is correct if you take the & out of the ini file. It also has problems with apostrophes and other chars. How can around this?

I cant get it to read any further.

Code: Select all

$ini = parse_ini_file('File.ini'); 
$un=$ini['UNITNAME'];

Code: Select all

[IBERTECH]
TWENTYFIVEHOURCLOCK=TRUE
AUTOEODCLOCKOUT=TRUE
EDITCLOCKIN=TRUE
;Directories
UNITNUMBER=1
UNITNAME=Joe's Pizza & Pasta
ADDRESS1=6860 S. Abc Way.
ADDRESS2=Greenwood,CO 80112
[REPORT]
PRINTFULLCARD=1
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Tried putting double quotes around the values?
303tech
Forum Commoner
Posts: 31
Joined: Sun Mar 11, 2007 3:25 pm

Post by 303tech »

in the parse_ini_file()? using "file.ini" instead of 'file.ini' ?

the INI is NOT able to change. the php must work around it.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Then I think you'll need to parse it yourself.
303tech
Forum Commoner
Posts: 31
Joined: Sun Mar 11, 2007 3:25 pm

Post by 303tech »

then how about using something like.

Code: Select all

<?php
$contents = file("File.ini");
$pos=strpos($contents, "UNITNAME=");
$string=substr($contents, $pos, 40);

$pos2=strpos($string, "="); 
echo substr($string, 9, 25);
?>
Problem is, some names are only five letters so then it will take the next line of INI into the string. How could I use regex to to only copy from position 9 till = \n (untill it reaches a space).
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

how about

Code: Select all

$ini = array();
foreach(file($ini_file) as $line)
	if(preg_match('/^([^;].*?)\s*=\s*(.*)/', trim($line), $m))
		$ini[$m[1]] = $m[2];
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I too was thinking regex - but you'll have to do it in multiple dimensions so you don't overwrite values from previous sections.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

After stripping the line of any comment and determining that it is not a section specifier explode() would work well, actually.
Post Reply