简体   繁体   English

如何使用PHP解析.pls文件? 遇到parse_ini_file问题

[英]How do I parse a .pls file using PHP? Having trouble with parse_ini_file

I have this file: 我有这个文件:

[playlist]
numberofentries=4
File1=http://108.61.73.119:8128
Title1=(#1 - 266/1000) 181.FM - POWER 181 -=[: The Hitz Channel :]=- www.181.fm
Length1=-1
File2=http://108.61.73.118:8128
Title2=(#2 - 318/1000) 181.FM - POWER 181 -=[: The Hitz Channel :]=- www.181.fm
Length2=-1
File3=http://108.61.73.117:8128
Title3=(#3 - 401/1000) 181.FM - POWER 181 -=[: The Hitz Channel :]=- www.181.fm
Length3=-1
File4=http://198.27.79.224:9770
Title4=(#4 - 27/50) 181.FM - POWER 181 -=[: The Hitz Channel :]=- www.181.fm
Length4=-1
Version=2

I'd like to parse it and get only file and title. 我想解析它,只获取文件和标题。 Problem is parse_ini_file gives me bogus error. 问题是parse_ini_file给我假的错误。 I tried normal way, like I would parse text file but it is becoming complicated with too much trimming. 我试过正常的方式,就像我会解析文本文件一样,但是修剪太多会变得复杂。

Any thoughts? 有什么想法吗?

php: PHP:

$streams = parse_ini_file("tunein-station.pls", true);
print_r($streams);

Error: 错误:

PHP Warning:  parse error in tunein-station.pls on line 4\n

Try using the INI_SCANNER_RAW scanner, something like this: 尝试使用INI_SCANNER_RAW扫描程序,如下所示:

parse_ini_file('playlist.ini', true, INI_SCANNER_RAW);

This should handle strings with spaces and [] s without " around them better. See scanner modes at the parse_ini_file() 's manual. 这应该处理带有空格和[] s的字符串,而不是"更好地围绕它们。请参阅parse_ini_file()手册中的扫描仪模式。

Alternatively, you can perform some preprocessing on the input file to make it a valid INI file: 或者,您可以对输入文件执行一些预处理,使其成为有效的INI文件:

<?php
// 1. Read your INI file into a string (here we hardcode it for the demo)
$str = <<<EOF
[playlist]
numberofentries=4
File1=http://108.61.73.119:8128
Title1=(#1 - 266/1000) 181.FM - POWER 181 -=[: The Hitz Channel :]=- www.181.fm
Length1=-1
File2=http://108.61.73.118:8128
Title2=(#2 - 318/1000) 181.FM - POWER 181 -=[: The Hitz Channel :]=- www.181.fm
Length2=-1
File3=http://108.61.73.117:8128
Title3=(#3 - 401/1000) 181.FM - POWER 181 -=[: The Hitz Channel :]=- www.181.fm
Length3=-1
File4=http://198.27.79.224:9770
Title4=(#4 - 27/50) 181.FM - POWER 181 -=[: The Hitz Channel :]=- www.181.fm
Length4=-1
Version=2
EOF;

// 2. Add quotes around the "title" values
$str = preg_replace('/^(Title\d+)=(.*)$/m', '\1="\2"', $str);

// 3. Now parse as INI
$array = parse_ini_string($str);

// 4. Here are your results:
print_r($array);
?>

Live demo 现场演示

Output: 输出:

Array
(
    [numberofentries] => 4
    [File1] => http://108.61.73.119:8128
    [Title1] => (#1 - 266/1000) 181.FM - POWER 181 -=[: The Hitz Channel :]=- www.181.fm
    [Length1] => -1
    [File2] => http://108.61.73.118:8128
    [Title2] => (#2 - 318/1000) 181.FM - POWER 181 -=[: The Hitz Channel :]=- www.181.fm
    [Length2] => -1
    [File3] => http://108.61.73.117:8128
    [Title3] => (#3 - 401/1000) 181.FM - POWER 181 -=[: The Hitz Channel :]=- www.181.fm
    [Length3] => -1
    [File4] => http://198.27.79.224:9770
    [Title4] => (#4 - 27/50) 181.FM - POWER 181 -=[: The Hitz Channel :]=- www.181.fm
    [Length4] => -1
    [Version] => 2
)

The only thing you have to be careful of here is double-quotes in the original title values; 这里唯一需要注意的是原始标题值中的双引号; you could use preg_replace_callback to fix this if you needed. 如果需要,你可以使用preg_replace_callback来解决这个问题。

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

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