0

I'm retriving a youtube link from mysql db with this code

<?php
//echo $embed;
$yt_url=$embed;
function get_youtube_id_from_url($url)
    {
    if (stristr($url,'youtu.be/'))
        { preg_match('/(https|http):\/\/(.*?)\/([a-zA-Z0-9_]{11})/i', $url, $final_ID);return $final_ID[3]; }
    else 
        { preg_match('/(https|http):\/\/(.*?)\/(embed\/|watch\?v=|(.*?)&v=|v\/|e\/|.+\/|watch.*v=|)([a-zA-Z0-9_]{11})/i', $url, $IDD); return $IDD[5]; }
    }
$variable= get_youtube_id_from_url($yt_url);
?>

and extracting the id part of the link and placing it in this piece of code below

echo "<div align='center'><iframe src=\"//www.youtube.com/embed/" . $variable. "\" style=\"background: #fff;\" frameborder=\"0\" height=\"300\" scrolling=\"auto\" width=\"480\"></iframe></div>";

but am told the vedio dose not exit, please help

4
Contributors
3
Replies
26
Views
3 Years
Discussion Span
Last Post by OsaMasw
0

The pattern in the preg match is missing the dash -, it should be [a-zA-Z0-9_-], so change the first to:

'/(https|http):\/\/(.*?)\/([a-zA-Z0-9_-]{11})/i'

And the second to:

'/(https|http):\/\/(.*?)\/(embed\/|watch\?v=|(.*?)&v=|v\/|e\/|.+\/|watch.*v=|)([a-zA-Z0-9_-]{11})/i'

Because in case of links as $yt_url = "http://youtu.be/--9oAhOSwpg"; it will not work.

1

Try this

<?php
$yt_url="http://www.youtube.com/watch?v=ZvzwthHh-IQ";
function get_youtube_id_from_url($url)
    {
        preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $url, $matches);
        return $matches[1];
    }
$variable= get_youtube_id_from_url($yt_url);

echo "<div align='center'><iframe src=\"//www.youtube.com/embed/" . $variable. "\" style=\"background: #fff;\" frameborder=\"0\" height=\"300\" scrolling=\"auto\" width=\"480\"></iframe></div>";
?>
Votes + Comments
This solved my problem before I asked
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.