简体   繁体   English

PHP parse_ini_file错误

[英]PHP parse_ini_file error

I am using parse_ini_file to read the contents of a file however it is not always successful. 我正在使用parse_ini_file读取文件的内容,但是它并不总是成功。

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: 但是,当我使用parse_ini_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 谢谢

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 ("). 如果ini文件中的值包含任何非字母数字字符,则需要将其用双引号(“)引起来。

PHP.net - parse_ini_file 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. 有保留字,不能将其用作ini文件的键。 These include: null, yes, no, true, false, on, off, none. 其中包括:null,yes,no,true,false,on,off,none。 Values null, no and false results in "", yes and true results in "1". 值null,no和false导致“”,yes和true导致“ 1”。 Characters ?{}|&~![()^" must not be used anywhere in the key and have a special meaning in the value 字符?{} |&〜![()^“不得在键的任何位置使用,并且在值中具有特殊含义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM