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

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


I am writing a simple blog for my website and I have succeeded in doing this.

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

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/$2\" allowfullscreen></iframe></div>
            ",
            $string
        );
    }

That is converting a youtube url(short or long) to an embed form.

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

Now what I wish to do is this:

现在我想做的是:

  • convert mp4 links, eg:
    http://cdn.example.com/akndh39/2016/askbdsjdbuu/my-video.mp4?id=28gsbd0mu&d=33hfsbb

    into

    转换mp4链接,例如:http://cdn.example.com/akndh39/2016/askbdsjdbuu/my-video.mp4?id = 28 gsbd0mu&d = 33 hfsbb

    <video width="320" height="240" controls> <source src="movie.mp4" type="video/mp4"> </video>

    <视频宽度="320"高="240"控制> < /视频>

  • convert mp3 links into equivalent tag format

    将mp3链接转换为等效的标签格式

However, I don't want to convert links such as this

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

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

I want them to stay intact like that, for both mp4 and mp3 links.

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

EDITED

编辑

What I want is this:

我想要的是:

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>

enter image description here

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>

1 个解决方案

#1


3  

This regex will find the mp4 URLs:

此regex将找到mp4 url:

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

After that there's some PHP code to replace the URL with the video and source tags. Note that this now captures leading quote characters to determine if the URL is contained in a href="" or src="" link; the code below explicitly checks for that and skips URLs found that start with quote characters. See the second URL in the first example to see how that works.

之后,有一些PHP代码可以用视频和源标记替换URL。注意,它现在捕获了引言字符,以确定URL是否包含在href=""或src="链接中;下面的代码显式地检查它,并跳过以引号字符开头的url。请参见第一个示例中的第二个URL,以了解其工作原理。

Here is the sample code using the URL to parse standalone MP4 URLs from text:

下面是使用URL解析独立MP4 URL的示例代码:

$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);
    }
}

The output for the first test case is:

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

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>"

The output for the second test case is:

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

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.
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2016/05/14/359debd102bb7c415d1db11ae4d3347a.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告