使用PHP

时间:2016-05-14 10:48:10

标签: php regex video html5-audio

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

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

即将youtube网址(短或长)转换为嵌入形式。

现在我想做的是:

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

    转入

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

  • 将mp3链接转换为等效标记格式

但是,我不想转换此链接

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

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 个答案:

答案 0 :(得分:3)

此正则表达式将找到mp4网址:

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

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

以下是使用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);
    }
}

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

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.