如何通过regex preg_match查找特定字符串

时间:2015-07-12 07:58:54

标签: php regex preg-match

我希望通过在php中应用preg_match来获取链接,这是我的代码

$string = playlist: [{
        image: "http://77.81.98.55/i/01/00012/d6ntku2p3tnn.jpg",
        provider: "http://static.vidzi.tv/nplayer/vidzi.swf",
        sources: [{
            file: "http://vidzi.tv/6c90fqha0k9i-4b4a926f75863405c12bfaeb331b331c.m3u8?embed=",
            type: 'hls'
        },{
        file: "http://77.81.98.55/gzuqiu4h342qedz7nknr5h3sli74im5vv7f2ae7z2pxjxhqnqxk4625rsdpq/v.mp4"
        }]
        ,tracks: [{file: "http://vidzi.tv/6c90fqha0k9i-4b4a926f75863405c12bfaeb331b331c.vtt", kind: "thumbnails"}]
    }]

preg_match('/file: "(.*?)",/i', $result, $dl_link);

echo '<pre>';
print_r($dl_link);
echo '</pre>';

输出

Array
(
    [0] => file: "http://vidzi.tv/6c90fqha0k9i-4b4a926f75863405c12bfaeb331b331c.m3u8?embed=",
    [1] => http://vidzi.tv/6c90fqha0k9i-4b4a926f75863405c12bfaeb331b331c.m3u8?embed=
)

从preg_match上面我得到第一个

file: "http://vidzi.tv/6c90fqha0k9i-4b4a926f75863405c12bfaeb331b331c.m3u8?embed="

但我想得到这个

file: "http://77.81.98.55/gzuqiu4h342qedz7nknr5h3sli74im5vv7f2ae7z2pxjxhqnqxk4625rsdpq/v.mp4"

帮助我谢谢

1 个答案:

答案 0 :(得分:0)

由于这似乎是JSON字符串,您可以使用此正则表达式:

preg_match_all('/file:\h*"([^"]*)"/i', $string, $m);

print_r($m[1][1]);
//=> http://77.81.98.55/gzuqiu4h342qedz7nknr5h3sli74im5vv7f2ae7z2pxjxhqnqxk4625rsdpq/v.mp4