Get all video qualities from a block of text using a regex
Asked Answered
H

1

0

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.

Handout answered 16/6, 2014 at 18:0 Comment(0)
F
2

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

Fullblown answered 16/6, 2014 at 18:3 Comment(1)
Answer with the perfect amount of detail, and beautifully formatted. :) +1Oiler

© 2022 - 2024 — McMap. All rights reserved.