使用正则表达式获取文本块中的所有视频质量

2022-03-29 00:00:00 php preg-match preg-match-all

我想从字符串获取所有视频质量。我的字符串:

#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=350000
128/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=750000
500/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1000000
750/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1250000
1000/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1750000
1500/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2750000
2500/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94

和我的PHP代码:

preg_match_all("/(.*?)/prog_index.m3u8/mis", $serviceurlget, $C);
print_r($C);

退货:

Array
(
    [0] => Array
        (
            [0] => #EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=350000
128/prog_index.m3u8
            [1] => ?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=750000
500/prog_index.m3u8
            [2] => ?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1000000
750/prog_index.m3u8
            [3] => ?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1250000
1000/prog_index.m3u8
            [4] => ?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1750000
1500/prog_index.m3u8
            [5] => ?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2750000
2500/prog_index.m3u8
        )

    [1] => Array
        (
            [0] => #EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=350000
128
            [1] => ?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=750000
500
            [2] => ?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1000000
750
            [3] => ?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1250000
1000
            [4] => ?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1750000
1500
            [5] => ?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2750000
2500
        )

)

但我不想要这样的结果。我希望它返回128 500 750 1000 1500 2500。我怎么才能做到这一点呢?我尝试了explode()函数。但这并不管用。我认为我的正则表达式代码有错误。


解决方案

preg_match_all()语句中删除ms修饰符。这些修饰符影响模式与主题字符串的匹配方式,并不总是必需的:

  • m修饰符将行锚(^$)的含义从"在字符串的开头/结尾匹配"更改为"在每个行的开头/结尾匹配"。如果主题字符串中没有换行符,或者正则表达式模式中没有出现行锚,则此修饰符无效。

  • s修饰符将点元字符(.)的含义从"匹配除换行符以外的所有字符"更改为"匹配所有包括换行符"。这使您可以将整个字符串视为单行。

有关详细信息,请参阅PHP manual documentation on Pattern Modifiers。


您的代码应该是:

preg_match_all("/(.*?)/prog_index.m3u8/i", $serviceurlget, $C);
print_r($C[1]);

输出:

Array
(
    [0] => 128
    [1] => 500
    [2] => 750
    [3] => 1000
    [4] => 1500
    [5] => 2500
)

Demo

相关文章