Convert youtube short url to full url

Hi everyone, I'm looking for an easy way to check if a string is equal to a URL:

http://youtu.be/WWQZ046NeUA

To convert it to the correct YouTube URL, follow these steps:

http://www.youtube.com/watch?v=WWQZ046NeUA

If you don't leave it alone, what is the easiest way to do this in php?

+4
source share
3 answers

You can use this call preg_replace:

$u = 'http://youtu.be/WWQZ046NeUA';
$r = preg_replace('~^https?://youtu\.be/([a-z\d]+)$~i', 'http://www.youtube.com/watch?v=$1', $u);
+4
source

str_replace must work miracles.

$url = ''; //url you're checking
$ytshorturl = 'youtu.be/';
$ytlongurl = 'www.youtube.com/watch?v=';
if (strpos($url,$yturl) !== false) {
    $url = str_replace($ytshorturl, $ytlongurl, $url);
}
+1
source

All Articles