PHP正则表达式|如何从另一条线拉出网址?

时间:2018-01-16 11:45:56

标签: php regex

我想提取包含index_5_av.m3u8

的链接

我正在使用正则表达式,但我得到了响应中的第一个链接:

/1364/1000055235_,16,180,400,800,1300,2000,3000,4500,_STAR.mp4.csmil/index_1_av.m3u8?null=0&id=AgBwxF67ZwMhBu+3XVrS4UWCkUpXl1hGzzRVtOIGbC4tLT5KmX0zX6fB0Z5qlHfurL7v7kKGtVdEJA%3d%3d

PHP:

$data = "#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=241000,RESOLUTION=320x180,CODECS="avc1.66.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://staragvod1-vh.akamaihd.net/i/videos/plus/sns/1364/1000055235_,16,180,400,800,1300,2000,3000,4500,_STAR.mp4.csmil/index_1_av.m3u8?null=0&id=AgBwxF67ZwMhBu+3XVrS4UWCkUpXl1hGzzRVtOIGbC4tLT5KmX0zX6fB0Z5qlHfurL7v7kKGtVdEJA%3d%3d
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=461000,RESOLUTION=416x234,CODECS="avc1.66.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://staragvod1-vh.akamaihd.net/i/videos/plus/sns/1364/1000055235_,16,180,400,800,1300,2000,3000,4500,_STAR.mp4.csmil/index_2_av.m3u8?null=0&id=AgBwxF67ZwMhBu+3XVrS4UWCkUpXl1hGzzRVtOIGbC4tLT5KmX0zX6fB0Z5qlHfurL7v7kKGtVdEJA%3d%3d
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=861000,RESOLUTION=640x360,CODECS="avc1.66.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://staragvod1-vh.akamaihd.net/i/videos/plus/sns/1364/1000055235_,16,180,400,800,1300,2000,3000,4500,_STAR.mp4.csmil/index_3_av.m3u8?null=0&id=AgBwxF67ZwMhBu+3XVrS4UWCkUpXl1hGzzRVtOIGbC4tLT5KmX0zX6fB0Z5qlHfurL7v7kKGtVdEJA%3d%3d
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1361000,RESOLUTION=720x404,CODECS="avc1.66.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://staragvod1-vh.akamaihd.net/i/videos/plus/sns/1364/1000055235_,16,180,400,800,1300,2000,3000,4500,_STAR.mp4.csmil/index_4_av.m3u8?null=0&id=AgBwxF67ZwMhBu+3XVrS4UWCkUpXl1hGzzRVtOIGbC4tLT5KmX0zX6fB0Z5qlHfurL7v7kKGtVdEJA%3d%3d
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2061000,RESOLUTION=1280x720,CODECS="avc1.77.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://staragvod1-vh.akamaihd.net/i/videos/plus/sns/1364/1000055235_,16,180,400,800,1300,2000,3000,4500,_STAR.mp4.csmil/index_5_av.m3u8?null=0&id=AgBwxF67ZwMhBu+3XVrS4UWCkUpXl1hGzzRVtOIGbC4tLT5KmX0zX6fB0Z5qlHfurL7v7kKGtVdEJA%3d%3d
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=3062000,RESOLUTION=1600x900,CODECS="avc1.77.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://staragvod1-vh.akamaihd.net/i/videos/plus/sns/1364/1000055235_,16,180,400,800,1300,2000,3000,4500,_STAR.mp4.csmil/index_6_av.m3u8?null=0&id=AgBwxF67ZwMhBu+3XVrS4UWCkUpXl1hGzzRVtOIGbC4tLT5KmX0zX6fB0Z5qlHfurL7v7kKGtVdEJA%3d%3d
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=4561000,RESOLUTION=1920x1080,CODECS="avc1.77.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://staragvod1-vh.akamaihd.net/i/videos/plus/sns/1364/1000055235_,16,180,400,800,1300,2000,3000,4500,_STAR.mp4.csmil/index_7_av.m3u8?null=0&id=AgBwxF67ZwMhBu+3XVrS4UWCkUpXl1hGzzRVtOIGbC4tLT5KmX0zX6fB0Z5qlHfurL7v7kKGtVdEJA%3d%3d";

$re = '/sns([^(]+?)\s/';
preg_match($re, $data, $matches);
echo $matches[1];

* $ data中的内容来自另一个网络服务器上的文件,

我编辑了,并添加了内容。

我如何只提取index_5_av.m3u8链接?

1 个答案:

答案 0 :(得分:3)

您不需要regexp进行如此简单的处理。将输入数据拆分为行filter contain the stringm modifier

index_5_av.m3u8

输出结果为:

$data = <<< E
#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=241000,RESOLUTION=320x180,CODECS="avc1.66.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://staragvod1-vh.akamaihd.net/i/videos/plus/sns/1364/1000055235_,16,180,400,800,1300,2000,3000,4500,_STAR.mp4.csmil/index_1_av.m3u8?null=0&id=AgBwxF67ZwMhBu+3XVrS4UWCkUpXl1hGzzRVtOIGbC4tLT5KmX0zX6fB0Z5qlHfurL7v7kKGtVdEJA%3d%3d
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=461000,RESOLUTION=416x234,CODECS="avc1.66.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://staragvod1-vh.akamaihd.net/i/videos/plus/sns/1364/1000055235_,16,180,400,800,1300,2000,3000,4500,_STAR.mp4.csmil/index_2_av.m3u8?null=0&id=AgBwxF67ZwMhBu+3XVrS4UWCkUpXl1hGzzRVtOIGbC4tLT5KmX0zX6fB0Z5qlHfurL7v7kKGtVdEJA%3d%3d
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=861000,RESOLUTION=640x360,CODECS="avc1.66.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://staragvod1-vh.akamaihd.net/i/videos/plus/sns/1364/1000055235_,16,180,400,800,1300,2000,3000,4500,_STAR.mp4.csmil/index_3_av.m3u8?null=0&id=AgBwxF67ZwMhBu+3XVrS4UWCkUpXl1hGzzRVtOIGbC4tLT5KmX0zX6fB0Z5qlHfurL7v7kKGtVdEJA%3d%3d
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1361000,RESOLUTION=720x404,CODECS="avc1.66.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://staragvod1-vh.akamaihd.net/i/videos/plus/sns/1364/1000055235_,16,180,400,800,1300,2000,3000,4500,_STAR.mp4.csmil/index_4_av.m3u8?null=0&id=AgBwxF67ZwMhBu+3XVrS4UWCkUpXl1hGzzRVtOIGbC4tLT5KmX0zX6fB0Z5qlHfurL7v7kKGtVdEJA%3d%3d
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2061000,RESOLUTION=1280x720,CODECS="avc1.77.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://staragvod1-vh.akamaihd.net/i/videos/plus/sns/1364/1000055235_,16,180,400,800,1300,2000,3000,4500,_STAR.mp4.csmil/index_5_av.m3u8?null=0&id=AgBwxF67ZwMhBu+3XVrS4UWCkUpXl1hGzzRVtOIGbC4tLT5KmX0zX6fB0Z5qlHfurL7v7kKGtVdEJA%3d%3d
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=3062000,RESOLUTION=1600x900,CODECS="avc1.77.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://staragvod1-vh.akamaihd.net/i/videos/plus/sns/1364/1000055235_,16,180,400,800,1300,2000,3000,4500,_STAR.mp4.csmil/index_6_av.m3u8?null=0&id=AgBwxF67ZwMhBu+3XVrS4UWCkUpXl1hGzzRVtOIGbC4tLT5KmX0zX6fB0Z5qlHfurL7v7kKGtVdEJA%3d%3d
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=4561000,RESOLUTION=1920x1080,CODECS="avc1.77.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://staragvod1-vh.akamaihd.net/i/videos/plus/sns/1364/1000055235_,16,180,400,800,1300,2000,3000,4500,_STAR.mp4.csmil/index_7_av.m3u8?null=0&id=AgBwxF67ZwMhBu+3XVrS4UWCkUpXl1hGzzRVtOIGbC4tLT5KmX0zX6fB0Z5qlHfurL7v7kKGtVdEJA%3d%3d
E;


$lines = explode("\n", $data);
$matches = array_filter(
    $lines,
    function($line) {
        return strpos($line, 'index_5_av.m3u8') !== false;
    }
);

print_r($matches);

奖金,键(Array ( [10] => https://staragvod1-vh.akamaihd.net/i/videos/plus/sns/1364/1000055235_,16,180,400,800,1300,2000,3000,4500,_STAR.mp4.csmil/index_5_av.m3u8?null=0&id=AgBwxF67ZwMhBu+3XVrS4UWCkUpXl1hGzzRVtOIGbC4tLT5KmX0zX6fB0Z5qlHfurL7v7kKGtVdEJA%3d%3d ) )告诉你匹配行的行号(用10开始计数)(在这种情况下为11 th 行。)

0解决方案:

regexp

打印所有匹配的行。 // Initialize $data as in the previous block of code $re = '#^http.*/index_5_av\.m3u8.*$#m'; $matches = array(); preg_match_all($re, $data, $matches); print_r($matches); 会查找以regexp开头且包含http的行。 preg_match_all()告诉Correct result/index_5_av.m3u8^与行的开头和结尾相匹配,而不是针对输入文本的开头和结尾(不是多行时的默认含义) 。)