Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert short youtube url to full url [closed]

Tags:

regex

php

youtube

Hi all i am looking for a simple way to check if a string equals an url like this:

http://youtu.be/WWQZ046NeUA

To convert it to a proper youtube url like this:

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

If not to leave it alone, what's the simplest way to do it in php?

like image 406
NaughtySquid Avatar asked Nov 15 '13 13:11

NaughtySquid


2 Answers

You can use this preg_replace call:

$u = 'http://youtu.be/WWQZ046NeUA';
$r = preg_replace('~^https?://youtu\.be/([a-z\d]+)$~i', 'http://www.youtube.com/watch?v=$1', $u);
like image 75
anubhava Avatar answered Sep 27 '22 21:09

anubhava


str_replace should work wonders.

$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);
}
like image 24
gator Avatar answered Sep 27 '22 22:09

gator