3

I am using parse_ini_file to read the contents of a file however it is not always successful.

For example, this file works fine:

[playlist]
numberofentries=3
File1=http://scfire-dtc-aa05.stream.aol.com:80/stream/1010
Title1=(#1 - 168/11500) Absolutely Smooth Jazz - SKY.FM - the world's smoothest jazz 24 hours a day
Length1=-1
File2=http://scfire-ntc-aa06.stream.aol.com:80/stream/1010
Title2=(#2 - 171/11500) Absolutely Smooth Jazz - SKY.FM - the world's smoothest jazz 24 hours a day
Length1=-1
File3=http://scfire-mtc-aa04.stream.aol.com:80/stream/1010
Title3=(#3 - 175/11500) Absolutely Smooth Jazz - SKY.FM - the world's smoothest jazz 24 hours a day
Length1=-1
Version=2

However when i use parse_ini_file to read the following file I get an error stating it cannot parse the file:

[playlist]
numberofentries=3
File1=http://87.230.82.17:80
Title1=(#1 - 365/1400) DEFJAY.DE - 100% R&B! (GERMANY)
Length1=-1
File2=http://87.230.56.25:80
Title2=(#2 - 370/1400) DEFJAY.DE - 100% R&B! (GERMANY)
Length1=-1
File3=http://87.230.56.32:80
Title3=(#3 - 375/1400) DEFJAY.DE - 100% R&B! (GERMANY)
Length1=-1
Version=2

Here's my code to read the files:

$file = "test.pls";
$ini_array = parse_ini_file($file, true);
$audiostream = $ini_array['playlist']['File1'];
echo "stream is: ".$audiostream;

I can't see much difference between the files. Anyone know what's going wrong?

Thanks

1
  • I'm just guessing here, but I suspect you actually got a proper error message, with a description of what went wrong and the location where it went wrong, which you have arbitrarily decided to withold from your question. Commented Jun 14, 2012 at 11:06

1 Answer 1

5

First you need to enclose your non-alphanumeric characters

From manual:

If a value in the ini file contains any non-alphanumeric characters it needs to be enclosed in double-quotes (").

PHP.net - parse_ini_file

So:

[playlist]
 numberofentries=3
 File1="http://87.230.82.17:80"
 Title1="(#1 - 365/1400) DEFJAY.DE - 100% R&B! (GERMANY)"
 Length1=-1
 File2="http://87.230.56.25:80"
 Title2="(#2 - 370/1400) DEFJAY.DE - 100% R&B! (GERMANY)"
 Length1=-1
 File3="http://87.230.56.32:80"
 Title3="(#3 - 375/1400) DEFJAY.DE - 100% R&B! (GERMANY)"
 Length1=-1
 Version=2 

Also please notice that there is reserved words:

There are reserved words which must not be used as keys for ini files. These include: null, yes, no, true, false, on, off, none. Values null, no and false results in "", yes and true results in "1". Characters ?{}|&~![()^" must not be used anywhere in the key and have a special meaning in the value

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thanks for your reply. This presents a problem for me as the file comes from an external source. Would it be possible to save the file, add quotes to everything and then try and parse it again?
sure, you can save change string and then use php.net/manual/en/function.parse-ini-string.php
Beautifully answered!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.