Normal9ja Normal9ja - 6 months ago 80
PHP Question

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.

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

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

  • convert mp3 links into equivalent tag format



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.

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>




Answer

This regex will find the mp4 URLs:

(["\']?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.

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

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