preg_replace Youtube links (regex)

Reply

Join Date: Mar 2008
Posts: 8
Reputation: syncy is an unknown quantity at this point 
Solved Threads: 1
syncy syncy is offline Offline
Newbie Poster

preg_replace Youtube links (regex)

 
0
  #1
Oct 29th, 2009
I'm really having some difficult wrapping my head around regex and preg_replace. What I want to do is have a function that scans an entire page for any <a href> tags pointing to Youtube and then convert them into an embedded youtube video.

Here's a code to show kinda of what I mean:
  1. <?php
  2. $youtubelink = "http://www.youtube.com/watch?v=Ysl6NCoshk8";
  3. $vid_section = substr( $youtubelink, strpos($youtubelink, '?') + 1, 1 );
  4.  
  5. $vid_id = substr( $youtubelink, strpos($youtubelink, '=') + 1 );
  6.  
  7. $embed_code = '<object width="560" height="340"><param name="movie" value="http://www.youtube.com/'.$vid_section.'/'.$vid_id.'&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/'.$vid_section.'/'.$vid_id.'&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>';
  8.  
  9. echo $embed_code;
  10. ?>

First of all I don't want to be providing the link, I want the function to search for it itself. So here's my stab at regex and preg_replace:
  1. echo preg_replace('#(?:<\>]+href=\")?(?:http://)?((?:[a-zA-Z]{1,4}\.)?youtube.com/(?:watch)?\?v=(.{11}?))[^"]*(?:\"[^\<\>]*>)?([^\<\>]*)(?:)?#', '<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/'.$matches[2].'"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'.$matches[2].'" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>', $ENTIREPAGE?);

Any idea where I'm going wrong?

To give a little more insight (if your bored enough to care), I have a tumblr blog that posts my twitter RSS feed updates. I sometimes post youtube links and would like them to show up as embedded videos for users to watch on the tumblr page.

Cheers.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,518
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 136
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Posting Virtuoso
 
0
  #2
Oct 30th, 2009
If you are talking about grabbing the youtube links from a page the following scripts will do that and display each video id in a list.
  1. <?php
  2. $input=file_get_contents('http://www.youtube.com/');
  3. preg_match_all('#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch\?v\=([-|~_0-9A-Za-z]+)&?.*?)#i',$input,$output);
  4. foreach ($output[3] AS $video_id) {
  5. echo $video_id.'<br>';
  6. }
  7. foreach ($output[4] AS $video_id) {
  8. echo $video_id.'<br>';
  9. }
  10. ?>
Last edited by cwarn23; Oct 30th, 2009 at 6:53 am. Reason: bugs
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*`
My favourite PC. - Oopy Doopy Do 2U2!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 8
Reputation: syncy is an unknown quantity at this point 
Solved Threads: 1
syncy syncy is offline Offline
Newbie Poster
 
0
  #3
Oct 31st, 2009
Awesome. Worked like a charm. Here's the final code I got:
  1. <?php
  2. $input=file_get_contents('http://warrendunlop.tumblr.com');
  3. preg_match_all('#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch\?v\=([-|~_0-9A-Za-z]+)&?.*?)#i',$input,$output);
  4. foreach ($output[4] AS $video_id) {
  5. $embed_code = '<object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/'.$video_id.'&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'.$video_id.'&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>';
  6. echo $embed_code.'<br>';
  7. }
  8. ?>

I am having a couple new problems. It seems to embedding doubles for each link on the blog page. Any way to check for a twin link? (you can see what I mean on my test page: www.warrendunlop.com/youtube_embed.php

I also am not sure how I would scan the current document instead of an external one.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,518
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 136
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Posting Virtuoso
 
0
  #4
Oct 31st, 2009
To only do each video id once the following is what the code would look like.
  1. <?php
  2. $input=file_get_contents('http://warrendunlop.tumblr.com');
  3. preg_match_all('#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch\?v\=([-|~_0-9A-Za-z]+)&?.*?)#i',$input,$output);
  4. foreach ($output[4] AS $video_id) {
  5. if (!isset($video[$video_id])) {
  6. $video[$video_id]=true;
  7. $embed_code = '<object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/'.$video_id.'&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'.$video_id.'&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>';
  8. echo $embed_code.'<br>';
  9. }
  10. }
  11. ?>
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*`
My favourite PC. - Oopy Doopy Do 2U2!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 8
Reputation: syncy is an unknown quantity at this point 
Solved Threads: 1
syncy syncy is offline Offline
Newbie Poster
 
0
  #5
Nov 1st, 2009
You are a legend sir.

Much thanks.
Reply With Quote Quick reply to this message  
Reply

Tags
php, youtube

Message:


Thread Tools Search this Thread



Tag cloud for php, youtube
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC