| | |
preg_replace Youtube links (regex)
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Mar 2008
Posts: 8
Reputation:
Solved Threads: 1
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:
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:
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.
Here's a code to show kinda of what I mean:
PHP Syntax (Toggle Plain Text)
<?php $youtubelink = "http://www.youtube.com/watch?v=Ysl6NCoshk8"; $vid_section = substr( $youtubelink, strpos($youtubelink, '?') + 1, 1 ); $vid_id = substr( $youtubelink, strpos($youtubelink, '=') + 1 ); $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>'; echo $embed_code; ?>
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)
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.
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.
php Syntax (Toggle Plain Text)
<?php $input=file_get_contents('http://www.youtube.com/'); preg_match_all('#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch\?v\=([-|~_0-9A-Za-z]+)&?.*?)#i',$input,$output); foreach ($output[3] AS $video_id) { echo $video_id.'<br>'; } foreach ($output[4] AS $video_id) { echo $video_id.'<br>'; } ?>
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/
My favourite PC. - MacGyver Fan
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*` My favourite PC. - MacGyver Fan
•
•
Join Date: Mar 2008
Posts: 8
Reputation:
Solved Threads: 1
0
#3 Oct 31st, 2009
Awesome. Worked like a charm. Here's the final code I got:
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.
PHP Syntax (Toggle Plain Text)
<?php $input=file_get_contents('http://warrendunlop.tumblr.com'); preg_match_all('#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch\?v\=([-|~_0-9A-Za-z]+)&?.*?)#i',$input,$output); foreach ($output[4] AS $video_id) { $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>'; echo $embed_code.'<br>'; } ?>
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.
0
#4 Oct 31st, 2009
To only do each video id once the following is what the code would look like.
php Syntax (Toggle Plain Text)
<?php $input=file_get_contents('http://warrendunlop.tumblr.com'); preg_match_all('#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch\?v\=([-|~_0-9A-Za-z]+)&?.*?)#i',$input,$output); foreach ($output[4] AS $video_id) { if (!isset($video[$video_id])) { $video[$video_id]=true; $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>'; echo $embed_code.'<br>'; } } ?>
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
My favourite PC. - MacGyver Fan
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*` My favourite PC. - MacGyver Fan
![]() |
Similar Threads
- how to remove space from string? (PHP)
- Embed Audio , Video content into website (PHP)
- Kids say the funniest things (Geeks' Lounge)
- I want to ALLOW line feeds, etc. (PHP)
- PHP - How to Enable Hyperlinks ONLY in Posts (PHP)
- This Music: You Should See This (Geeks' Lounge)
Other Threads in the PHP Forum
- Previous Thread: PHP call DLL
- Next Thread: PHP Error on line
| Thread Tools | Search this Thread |
access adobe advanced ajax apple array asp bbc beginner broadcast business class cms contentsharing copyright curl data database developer developers development display displayads drama dropdown ecommerce email errorlog facebook fairuse files flash form forms function google html image include integration internet itunes java javascript jquery lamp limit link linux login mail marketing menu methods mobile motrin multiple music mysql news online oop paypal php politics post privacy query riaa script search security session sms soap socialmedia socialnetworking spam sql stats tutorial tv twitter ubuntu uk upload validation vbulletin viacom video viralmarketing web web2.0 webdesign website wpf xml y!os youtube zend







