I am currently trying to retrieve YouTube video IDs through url. I created a function getYoutubeVideoID
that strips $url
and finds the $sVideoID
. The Problem is that when i echo the variable i get an empty value of $sVideoID. How come i am getting no value result for $sVideoID
if i am assigning it the video ID?
<?php
if($_POST)
{
$url = $_POST['yurl'];
function getYoutubeVideoID($url) {
$sVideoID = preg_replace('~https?://(?:[0-9A-Z-]+\.)?(?:youtu\.be/| youtube\.com\S*[^\w\-\s])([\w\-]{11})
(?=[^\w\-]|$)(?![?=&+%\w]*(?:[\'"][^<>]*>| </a>))[?=&+%\w-]*~ix','<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>',$url);
return $sVideoID;
}
$hth = 300; //$_POST['yheight'];
$wdth = 500; //$_POST['ywidth'];
?>
<?
//Iframe code
echo htmlentities ('<iframe src="http://www.youtube.com/embed/'.$sVideoID.'" frameborder="0" width="'.$wdth.'" height="'.$hth.'"></iframe>');
?>
<?
//Old way to embed code
echo htmlentities ('<embed src="http://www.youtube.com/v/'.$sVideoID.'" width="'.$wdth.'" height="'.$hth.'" type="application/x-shockwave-flash" wmode="transparent" embed="" /></embed>');
}
?>