2

imagine this url:

http://www.youtube.com/watch?v=6n8PGnc_cV4&feature=rec-LGOUT-real_rn-2r-13-HM

what is the cleanest and best regexp to do the following:

1.) i want to strip off every thing after the video URL. so that only http://www.youtube.com/watch?v=6n8PGnc_cV4 remains.

2.) i want to convert this url into http://www.youtube.com/v/6n8PGnc_cV4

Since i'm not much of a regexp-ert i need your help:

$content = preg_replace('http://.*?\?v=[^&]*', '', $content); 

return $content;

edit: check this out! I want to create a really simple WordPress plugin that just recognizes every normal youtube URL in my $content and replaces it with the embed code:

<?php
function videoplayer($content) {
    
    $embedcode = '<object class="video" width="308" height="100"><embed src="' . . '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="308" height="100" wmode="opaque"></embed></object>';
    
    //filter normal youtube url like http://www.youtube.com/watch?v=6n8PGnc_cV4&feature=rec-LGOUT-real_rn-2r-13-HM
    //convert it to http://www.youtube.com/v/6n8PGnc_cV4
    //use embedcode and pass along the new youtube url
    $content = preg_replace('', '', $content); 
    
    //return embedcode
    return $content;
}

add_filter('the_content', 'videoplayer');  
?>
3ICE
  • 54
  • 6
matt
  • 42,713
  • 103
  • 264
  • 397

4 Answers4

0

I use this search criteria in my script:

/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/
Peter Badida
  • 11,310
  • 10
  • 44
  • 90
Rasmus Styrk
  • 1,296
  • 2
  • 20
  • 36
  • so how do i use this in my function above to get what i want? i need to strip off all additional parameters and transform /watch?v= into /v/ – matt Feb 12 '11 at 09:16
0

You could just split it on the first ampersand.

$content = explode('&', $content);
$content = $content[0];
Sam Dufel
  • 17,560
  • 3
  • 48
  • 51
  • The only problem with this is if the paramters are out of order. E.g. https://www.youtube.com/watch?feature=player_embedded&v=EhiMrEvfvo4. – mehulkar Jun 25 '13 at 17:45
  • Youtube urls are in a pretty regular order, seeing as how they're generally generated by one of the generators on the site. – Sam Dufel Jun 25 '13 at 20:11
  • However, looking at the edit the OP made after I put this answer in, it's clear he's looking for something different... – Sam Dufel Jun 25 '13 at 20:11
0

Edit: Simplest regexp: /http:\/\/www\.youtube\.com\/watch\?v=.*/

Youtube links are all the same. To get the video id from them, first you slice off the extra parameters from the end and then slice off everything but the last 11 characters. See it in action:

$url = "http://www.youtube.com/watch?v=1rnfE4eo1bY&feature=...";
$url = $url.left(42); // "http://www.youtube.com/watch?v=1rnfE4eo1bY"
$url = $url.right(11); // "1rnfE4eo1bY"
$result = "http://www.youtube.com/v/" + $url; // "http://www.youtube.com/v/1rnfE4eo1bY"

You can uniformize all your youtube links (by removing useless parameters) with a Greasemonkey script: http://userscripts.org/scripts/show/86758. Greasemonkey scripts are natively supported as addons in Google Chrome.

And as a bonus, here is a one (okay, actually two) liner:

$url = "http://www.youtube.com/watch?v=1rnfE4eo1bY&feature=...";
$result = "http://www.youtube.com/v/" + $url.left(42).right(11);

--3ICE

3ICE
  • 54
  • 6
  • 1
    Not even close. YouTube links come in many formats: http://stackoverflow.com/questions/5830387/php-regex-find-all-youtube-video-ids-in-string/5831191#5831191 – sscirrus Oct 12 '11 at 05:28
  • That was then. Since 2010 all links conform to one of these two templates: htt**p**://www.youtube.com/watch?v=* or http**s**://www.youtube.com/watch?v=* Examples from your link: The youtu.be short format is always expanded to the full URL now. User and channel page links don't work anymore for watching videos. (I never liked those links anyway.) Some redirect to the proper URL, others just show the channel of whoever uploaded the linked video. Additional parameters always appear after the initial v param now. There were probably no people who used the nocookie domain, it doesn't work anymore. – 3ICE Nov 29 '14 at 17:51
-1
$url = "http://www.youtube.com/v/6n8PGnc_cV4";
$start = strpos($url,"v=");
echo 'http://www.youtube.com/v/'.substr($url,$start+2);
Emyr
  • 2,351
  • 18
  • 38
  • i don't have this $url like this! i need a regexp to filter $content for any given youtubeurl. then convert it to this /v/ url and return it inside of an embedcode. check out my edits! – matt Feb 12 '11 at 09:39
  • So it's a WP plugin? What's wrong with http://wordpress.org/extend/plugins/smart-youtube/ or http://wordpress.org/extend/plugins/wp-youtube/ ? – Emyr Feb 13 '11 at 23:31
  • the thing is i want to build my own one, in order to use my own embed code. i guess it shoudn't be that hard. it's just 1.) checking if a string matches a youtube pattern and 2.) return it inside an embedcode. – matt Feb 14 '11 at 10:14