使用 PHP 在 html 内容中自动检测并嵌入 MP4/MP3 链接

Automatically detect and embed MP4/MP3 Links in html content using PHP

我正在为我的网站写一个简单的博客,我已经成功了。

function convertYoutube($string, $id='') {
        return preg_replace(
            "/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i",
            "<div class=videoWrapper><iframe style=\"clear:both;\" width=\"420\" height=\"315\" src=\"//www.youtube.com/embed/\" allowfullscreen></iframe></div>
            ",
            $string
        );
    }

这就是将 youtube url(短或长)转换为嵌入形式。

现在我想做的是:

但是,我不想转换这样的链接

<a href="http://cdn.example.com/akndh39/2016/askbdsjdbuu/my-video.mp4">DOWNLOAD VIDEO</a>

我希望它们保持原样,对于 mp4 和 mp3 链接。

已编辑

我要的是这个:

This is a very nice Video by Artiste So-n-So    https://scontent.cdninstagram.com/t50.2886-16/13229628_1095069080586346_1424116369_n.mp4

<a href="http://cdn.example.com/akndh39/2016/askbdsjdbuu/my-video.mp4?id=28gsbd0mu&d=33hfsbb">
Download Video</a>

This is a very nice Video by Artiste So-n-So     <video width="320" height="240" controls>
<source src="https://scontent.cdninstagram.com/t50.2886-16/13229628_1095069080586346_1424116369_n.mp4" type="video/mp4">
</video>

<a href="http://cdn.example.com/akndh39/2016/askbdsjdbuu/my-video.mp4?id=28gsbd0mu&d=33hfsbb">
Download Video</a>

此正则表达式将找到 mp4 URLs:

(["\']?https?:\/\/[^\/]+(?:\/[^\/]+)*?.mp4\?(&?\w+(=[^&\'"\s]*)?)*)

之后有一些 PHP 代码用视频和源标签替换 URL。请注意,这现在捕获前导引号字符以确定 URL 是否包含在 href=""src="" link 中;下面的代码明确检查并跳过发现的 URLs 以引号字符开头。请参阅第一个示例中的第二个 URL 以了解其工作原理。

这是使用 URL 从文本中解析独立 MP4 URLs 的示例代码:

$subject = <<<LABEL
    This is a very nice Video by Artiste So-n-So    https://s...content-available-to-author-only...m.com/t50.2886-16/13229628_1095069080586346_1424116369_n.mp4


        <a href="http://c...content-available-to-author-only...e.com/akndh39/2016/askbdsjdbuu/my-video.mp4?id=28gsbd0mu&d=33hfsbb">
        Download Video</a>"
LABEL;

$subject = <<<LABEL
Please one more thing, can you expand your regex to include URLs like these.
https://z-1-scontent-lhr3-1.xx.fbcdn.net/v/t42.1790-4/13226507_264732453878764_‌​308543824_n.mp4?efg=eyJ2ZW5jb2RlX3RhZyI6InN2ZV9zZCJ9&oh=c68eadae7d9a5b25ad290ea72‌​3f8fc40&oe=57378FA2 
that is URLs that doesn't end immediately with the extension, but have other parameters added to it.
LABEL;

$pattern = '(["\']?https?:\/\/[^\/]+(?:\/[^\/]+)*?.mp4\?(&?\w+(=[^&\'"\s]*)?)*)';
$matches = Array();
$match = preg_match($pattern, $subject, $matches);

if (count($matches) !== 0) {
    $first_char = substr($matches[0], 0, 1);
    if ($first_cahr !== '"' && $first_char !== "'") {
        $replace = '<video width="320" height="240" controls><source src="' . $matches[0] . '" type="video/mp4"></video>';

        $result = str_replace($matches[0], $replace, $subject);

        print_r($result);
    }
}

第一个测试用例的输出是:

Success time: 0.03 memory: 52480 signal:0
    This is a very nice Video by Artiste So-n-So    <video width="320" height="240" controls><source src="https://scontent.cdninstagram.com/t50.2886-16/13229628_1095069080586346_1424116369_n.mp4" type="video/mp4"></video>


        <a href="http://cdn.example.com/akndh39/2016/askbdsjdbuu/my-video.mp4?id=28gsbd0mu&d=33hfsbb">
        Download Video</a>"

第二个测试用例的输出是:

Please one more thing, can you expand your regex to include URLs like these.
<video width="320" height="240" controls><source src="https://z-1-scontent-lhr3-1.xx.fbcdn.net/v/t42.1790-4/13226507_264732453878764_‌​308543824_n.mp4?efg=eyJ2ZW5jb2RlX3RhZyI6InN2ZV9zZCJ9&oh=c68eadae7d9a5b25ad290ea72‌​3f8fc40&oe=57378FA2" type="video/mp4"></video> 
that is URLs that doesn't end immediately with the extension, but have other parameters added to it.