Reputation: 11
I have a string that contains an address to a youtube video, I want to use this to display the video in a pop-up lightbox. In the current form the link will not work in the lightbox:
http://www.youtube.com/v/CD2LRROpph0?f=videos&c=TEST&app=youtube_gdata&version=3
I has an idea to extract the video id 'CD2LRROpph0' and just append that to a regular youtube url, for example
http://www.youtube.com/watch?v=CD2LRROpph0.
Which i know works in the lightbox.
Any ideas on how to extract this code from the string???
Upvotes: 1
Views: 72
Reputation: 490243
This one will handle different protocols and different YouTube URLs (in case YouTube come out with country specific TLDs, for example).
$urlTokens = parse_url($url);
$newUrl = $urlTokens['scheme'] . '://' . $urlTokens['host'] . '/watch?v=' . preg_replace('~^/v/~', '', $urlTokens['path']);
Upvotes: 1
Reputation: 619
Or try this, you get an array back and can use the query you want http://www.php.net/manual/en/function.parse-url.php
Upvotes: 0
Reputation: 39356
$newUrl = preg_replace('@http://www.youtube.com/v/([a-z0-9_\-]{11}).*$@i',
'http://www.youtube.com/watch?v=$1', $string);
Upvotes: 0