<?php
$m3u = '#EXTM3U
#EXTINF:0 tvg-name="BBC" audio-track="eng" tvg-logo="http://mylogos.domain/BBC.png", BBC World
http://server.name/stream/to/video2
#EXTINF:0 tvg-name="CNN" audio-track="rus", CNN International
http://server.name/stream/to/video2
#EXTINF:0, Arirang
http://server.name/stream/to/video3
#EXTINF:-1 tvg-id="http://www.arte.tv/" tvg-name="|ALLEMAGNE| ARTE ASTRA 19.2" tvg-logo="http://192.168.1.150/logotheque/arte_de.png" group-title="CULTURE ALLEMAGNE ASTRA 19.2",|ALLEMAGNE| ARTE ASTRA 19.2
http://192.168.1.150/1258';
$pattern = '~(*BSR_ANYCRLF)
^ \Q#EXTINF:\E
(?<length> 0 | -1 | [1-9][0-9]* ) [ ]*
(?<attributes> (?: [ ]* [\w-]+ =" [^"]* " \B )+ )? , [ ]*
(?<title> .* \S ) [ ]* \R
(?<url> .* \S )
~xum';
preg_match_all($pattern, $m3u, $matches, PREG_SET_ORDER | PREG_UNMATCHED_AS_NULL);
$results = [];
$keys = array_flip(['length', 'attributes', 'title', 'url']);
foreach($matches as $m) {
if ( $m['attributes'] !== null ) {
preg_match_all('~(?<name> [\w-]+ ) = " (?<value> [^"]* ) " ~x', $m['attributes'], $attr);
$m['attributes'] = array_combine($attr['name'], $attr['value']);
}
$results[] = array_intersect_key($m, $keys);
}
print_r($results);
- Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- Array
(
[0] => Array
(
[length] => 0
[attributes] => Array
(
[tvg-name] => BBC
[audio-track] => eng
[tvg-logo] => http://mylogos.domain/BBC.png
)
[title] => BBC World
[url] => http://server.name/stream/to/video2
)
[1] => Array
(
[length] => 0
[attributes] => Array
(
[tvg-name] => CNN
[audio-track] => rus
)
[title] => CNN International
[url] => http://server.name/stream/to/video2
)
[2] => Array
(
[length] => 0
[attributes] =>
[title] => Arirang
[url] => http://server.name/stream/to/video3
)
[3] => Array
(
[length] => -1
[attributes] => Array
(
[tvg-id] => http://www.arte.tv/
[tvg-name] => |ALLEMAGNE| ARTE ASTRA 19.2
[tvg-logo] => http://192.168.1.150/logotheque/arte_de.png
[group-title] => CULTURE ALLEMAGNE ASTRA 19.2
)
[title] => |ALLEMAGNE| ARTE ASTRA 19.2
[url] => http://192.168.1.150/1258
)
)
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Array
(
[0] => Array
(
[length] => 0
[attributes] => Array
(
[tvg-name] => BBC
[audio-track] => eng
[tvg-logo] => http://mylogos.domain/BBC.png
)
[title] => BBC World
[url] => http://server.name/stream/to/video2
)
[1] => Array
(
[length] => 0
[attributes] => Array
(
[tvg-name] => CNN
[audio-track] => rus
)
[title] => CNN International
[url] => http://server.name/stream/to/video2
)
[2] => Array
(
[length] => 0
[attributes] =>
[title] => Arirang
[url] => http://server.name/stream/to/video3
)
[3] => Array
(
[length] => -1
[attributes] => Array
(
[tvg-id] => http://www.arte.tv/
[tvg-name] => |ALLEMAGNE| ARTE ASTRA 19.2
[tvg-logo] => http://192.168.1.150/logotheque/arte_de.png
[group-title] => CULTURE ALLEMAGNE ASTRA 19.2
)
[title] => |ALLEMAGNE| ARTE ASTRA 19.2
[url] => http://192.168.1.150/1258
)
)
preferences:
161.99 ms | 405 KiB | 156 Q