首页 > 解决方案 > 使用 preg_match_all 从 html 页面获取 mp3 url

问题描述

我正在尝试从 html 页面中提取 mp3 链接,但我的 preg_match_all 不起作用?

PHP CURL 页面上的 HTML 示例

keyEnabled: true,
remainingDuration: true,
toggleDuration: true,
ready: function (event) {
    $(this).jPlayer("setMedia", {
        title: "",
        mp3: "http://example.com/vod/test.mp3",
    });
}

我想得到http://example.com/vod/test.mp3

$Pattern = '/((https?:\/\/)?(\w+?\.)+?(\w+?\/)+\w+?.(mp3|ogg))/im';
preg_match_all($Pattern,htmlentities($html),$Matches);
var_dump($Matches);

什么都没有返回,我该怎么做?

示例页面: https ://3v4l.org/G4sLF

标签: phpregex

解决方案


在您的 URL 中,您有一个-不包含在\w

http://ns1.indexforce.com/vod/barg-i_sabz_1.mp3
//                         here __^

使用这个正则表达式:

$Pattern = '~mp3:\h+"((https?://)?\S+\.(mp3|ogg))"~im';

推荐阅读