Page 1 of 1

parse_ini_file(), not accepting ampersands (&)

Posted: Fri Mar 30, 2007 2:23 pm
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

Posted: Fri Mar 30, 2007 3:21 pm
by feyd
Tried putting double quotes around the values?

Posted: Fri Mar 30, 2007 4:43 pm
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.

Posted: Fri Mar 30, 2007 4:51 pm
by feyd
Then I think you'll need to parse it yourself.

Posted: Fri Mar 30, 2007 6:19 pm
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).

Posted: Fri Mar 30, 2007 6:37 pm
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];

Posted: Fri Mar 30, 2007 7:34 pm
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.

Posted: Fri Mar 30, 2007 11:11 pm
by feyd
After stripping the line of any comment and determining that it is not a section specifier explode() would work well, actually.