dongzhong6675 2014-06-16 18:00
浏览 71
已采纳

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

I want to get all video qualitys from string. My string:

#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

And my PHP code:

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

Returns:

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
        )

)

But I don't want this result. I want it to return 128 500 750 1000 1500 2500. How can I do it? I tried explode() function. But it didn't work. I think there is a mistake with my regex code.

  • 写回答

1条回答 默认 最新

  • douan3019 2014-06-16 18:03
    关注

    Remove the m and s modifiers from the preg_match_all() statement. These modifiers affect how the pattern matches the subject string, and are not always required:

    • m modifier changes the meaning of the line anchors (^ and $) from "match at the beginning/end of the string" to "match at the beginning/end of each line". If there are no newline characters in the subject string, or no occurrences of line anchors in the regex pattern, this modifier is useless.

    • s modifier changes the meaning of the dot meta-character (.) from "match everything except newline characters" to "match everything including newline characters". This allows you to treat the whole string as a single line.

    See the PHP manual documentation on Pattern Modifiers for more information.


    Your code should be:

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

    Output:

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

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理