如何检索变量=“值"来自 m3u 字符串的对 [英] How to retrieve variable="value" pairs from m3u string
问题描述
我有 m3u 文件,其中包含像示例这样的行:
I have m3u file that contain lines like example:
#EXTINF:0 $ExtFilter="Viva" group-title="Variedades" tvg-logo="logo/Viva.png" tvg-name="Viva"
我在 PHP 中运行它但没有成功:
I run this in PHP with no success:
preg_match('/([a-z0-9\-_]+)=\"([a-z0-9\-_\s.]+)\"\s+/i',$str,$matches)
我想得到:
$matches[0] = $ExtFilter
$matches[1] = Viva
$matches[2] = group-title
$matches[3] = Variedades
$matches[4] = tvg-logo
$matches[5] = logo/Viva.png
$matches[6] = tvg-name
$matches[7] = Viva
我尝试使用正则表达式工具(例如 this).
I try regexp tools (like this).
谢谢你.
推荐答案
使用 preg_match_all
执行多个匹配:
Use preg_match_all
to perform multiple matches:
preg_match_all('/([\w-]+)="([\w-\s.\/]+)"/i',$str,$matches, PREG_SET_ORDER);
它以二维数组形式返回结果——一维是匹配,另一维是匹配中的捕获组.要将它们按照您想要的结果放入单个数组中,请使用循环:
It returns the results as a 2-dimensional array -- one dimension is the match, another dimension is the capture groups within the matches. To get them into a single array as in your desired result, use a loop:
$results = array();
foreach ($matches as $match) {
array_push($results, $match[1], $match[2]);
}
print_r($results);
打印:
Array
(
[0] => ExtFilter
[1] => Viva
[2] => group-title
[3] => Variedades
[4] => tvg-logo
[5] => logo/Viva.png
[6] => tvg-name
[7] => Viva
)
我使用 \w
代替 a-z0-9_
简化了您的正则表达式.我还将 /
添加到第二个字符集,以便 logo/Viva.png
匹配.最后我去掉了 \s+
,因为它阻止了最后一个变量赋值的工作.
I simplified your regexp by using \w
in place of a-z0-9_
. I also added /
to the second character set, so that logo/Viva.png
would match. I got rid of \s+
at the end, because it prevented the last variable assignment from working.
这篇关于如何检索变量=“值"来自 m3u 字符串的对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!