944,030 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 7424
  • PHP RSS
Oct 29th, 2009
0

preg_replace Youtube links (regex)

Expand Post »
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:
PHP Syntax (Toggle Plain Text)
  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:
PHP Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
syncy is offline Offline
8 posts
since Mar 2008
Oct 30th, 2009
0
Re: preg_replace Youtube links (regex)
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.
php Syntax (Toggle Plain Text)
  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
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Oct 31st, 2009
0
Re: preg_replace Youtube links (regex)
Awesome. Worked like a charm. Here's the final code I got:
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
syncy is offline Offline
8 posts
since Mar 2008
Oct 31st, 2009
0
Re: preg_replace Youtube links (regex)
To only do each video id once the following is what the code would look like.
php Syntax (Toggle Plain Text)
  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. ?>
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Nov 1st, 2009
0
Re: preg_replace Youtube links (regex)
You are a legend sir.

Much thanks.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
syncy is offline Offline
8 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Posting data from x to y to be processed ?
Next Thread in PHP Forum Timeline: PHP Error on line





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC