PHP - Convert youtu.be URLs to youtube.com
Youtube has introduced a new URL shorthand – youtu.be
For example youtu.be/DIArJjU8HjE takes you to
youtube.com/watch?v=DIArJjU8HjE&feature=youtu.be
Annoyingly this means that any users pasting a youtu.be URL into your video parsing scripts will be told it doesn’t work, so we’d best get updating those scripts!
Pop this in before you start processing the url, where $url is, not surprisingly, the url to be parsed.
//dMb 21/4/2011 Hack to handle youtu.be URLs dave@absolutedisaster.co.uk $needle = '/^(http:\/\/)*(www\.)*(youtu\.be)\/([A-Za-z0-9]{11})$/'; preg_match($needle, $url, $result); if($result[3] == 'youtu.be'){ $url = 'http://www.youtube.com/watch?v=' . $result[4]; } //dMb end hack
[update 20110720-1657] While implementing this and similar for grabbing youtube thumbnails today I suddenly though, what if youtube ever starts using https? Well, the odds may be slim, but the costs of allowing for it are negligible, so why take the risk?
//dMb 21/4/2011 Hack to handle youtu.be URLs dave@absolutedisaster.co.uk //dMb 20/7/2011 Updated to allow for https $needle = '/^(http(s?):\/\/)*(www\.)*(youtu\.be)\/([A-Za-z0-9]{11})$/'; preg_match($needle, $url, $result); if($result[4] == 'youtu.be'){ $url = 'http' . $result[2] . '://www.youtube.com/watch?v=' . $result[5]; } //dMb end hack
[/update]
That particular link is the fantastic new video from my friends Six Toes – currently featured on the front of the Depeche Mode website.
