【问题标题】:PHP parse_ini_file errorPHP parse_ini_file 错误
【发布时间】:2012-06-17 09:37:59
【问题描述】:

我正在使用 parse_ini_file 来读取文件的内容,但它并不总是成功。

例如,这个文件可以正常工作:

[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

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

这是我读取文件的代码:

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

我看不出文件之间有太大区别。有谁知道怎么回事?

谢谢

【问题讨论】:

  • 我只是在这里猜测,但我怀疑您实际上收到了一条正确的错误消息,其中描述了出错的地方和出错的位置,您已任意决定从您的问题。

标签: php parsing ini


【解决方案1】:

首先您需要将您的非字母数字字符

括起来

来自手册:

如果 ini 文件中的值包含任何非字母数字字符,则需要将其括在双引号 (") 中。

PHP.net - 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 

另外请注意有保留字:

有些保留字不能用作 ini 文件的键。其中包括:null、yes、no、true、false、on、off、none。值 null、no 和 false 导致“”,yes 和 true 导致“1”。字符 ?{}|&~![()^" 不得在键中的任何位置使用,并且在值中具有特殊含义

【讨论】:

  • 您好,感谢您的回复。这给我带来了一个问题,因为该文件来自外部来源。是否可以保存文件,为所有内容添加引号,然后再次尝试解析它?
  • 当然,您可以保存更改字符串,然后使用php.net/manual/en/function.parse-ini-string.php
  • 回答得很好!
猜你喜欢
  • 2013-05-10
  • 2011-08-09
  • 2015-05-06
  • 1970-01-01
  • 2012-12-11
  • 2023-03-16
  • 2010-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多