youtube auto-embedding for phpBB

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

youtube auto-embedding for phpBB

Post by Kieran Huggins »

I just wrote some code for a phpBB installation (I guess it's a MOD, sort-of) that I thought people could pitch-in to if they find it useful:

This MOD replaces URLs to youTube videos with their embedded counterparts.

Edit 'includes/bbcode.php' and find:

Code: Select all

function make_clickable($text)
{
    $text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text);
 
    // pad it with a space so we can match things at the start of the 1st line.
    $ret = ' ' . $text;
then add:

Code: Select all

   // youtube code..
    $ret = preg_replace("#(^|[\n ]|<a(.*?)>)http://(www\.)?youtube\.com/watch\?v=([a-zA-Z0-9\-_]+)(</a>)?#im",' <!-- BEGIN youtube --><object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/$4"></param><embed src="http://www.youtube.com/v/$4" type="application/x-shockwave-flash" width="425" height="350"></embed></object><br /><a href="http://youtube.com/watch?v=$4" target="_blank">http://youtube.com/watch?v=$4</a><!-- END youtube -->', $ret);
This works with plaintext URLs and already linked URLs, and with or without the "www".

If anyone wants to improve this code, my suggestion would be to not embed the video if it's quoted.

Any takers?

Maybe after we're done here one of the mods would drop this in the snippets section?

Cheers,
Kieran
Last edited by Kieran Huggins on Sat Feb 02, 2008 5:19 am, edited 1 time in total.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

If someone linked to the youtube video, they probably want it to stay a link. This should be implemented as a custom BBcode tag.
Neno
Forum Newbie
Posts: 3
Joined: Sat Dec 30, 2006 8:46 am

Post by Neno »

Hi,
youtube auto-embedding is perfect for me. Could you write a similar code for google videos auto-embedding?
Thank you
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I'll make you a deal - if you try to write it and post it here, I'll help fix it if it's broken 8)
Neno
Forum Newbie
Posts: 3
Joined: Sat Dec 30, 2006 8:46 am

Post by Neno »

:D OK I've tried

Code: Select all

$ret = preg_replace("#(^|[\n ]|<a(.*?)>)http://video.google.com/videoplay\?docid=([a-zA-Z0-9\-_]+)(</a>)?#im",'<!-- BEGIN GVideo --><object width="425" height="350"><param name="movie" value="http://video.google.com/googleplayer.swf?docId=$4"></param><embed src="http://video.google.com/googleplayer.swf?docId=$4" type="application/x-shockwave-flash" width="425" height="350"></embed></object></p><!-- END Gvideo -->', $ret);
The first part is ok, I can see the player, but don't know how to link a movie... :?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

not quite.. if you check out google's embed code, it's quite different from youtube:

Code: Select all

// google video code...
$ret = preg_replace("#(^|[\n ]|<a(.*?)>)http://video.google.com/videoplay\?docid=([0-9]+)(</a>)?#im",'<!-- BEGIN GVideo --><embed style="width:400px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=$3"> </embed><br /><a href="http://video.google.com/videoplay?docid=$3" target="_blank">http://video.google.com/videoplay?docid=$3</a><!-- END Gvideo -->', $ret);
also, the "docid" is only numeric in GV's case
Last edited by Kieran Huggins on Sat Feb 02, 2008 5:19 am, edited 1 time in total.
Neno
Forum Newbie
Posts: 3
Joined: Sat Dec 30, 2006 8:46 am

Post by Neno »

Great!
Thank you very much
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post by thiscatis »

freebies for all!.
Would be cool for a newer version that you just have to select from which video hosting site your linking (like the 5 biggest one)
instead of having 5 mods
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

thiscatis wrote:freebies for all!.
Would be cool for a newer version that you just have to select from which video hosting site your linking (like the 5 biggest one)
instead of having 5 mods
You can just paste these next to one another in 'includes/bbcode.php'... I don't know how to make it any simpler :-)
tvleeuwen
Forum Newbie
Posts: 1
Joined: Sun Mar 25, 2007 7:58 am

Post by tvleeuwen »

Thanks for the code.
little modifications for your sripts so it works with al language codes. for my with video.google.nl

Code: Select all

// google video code... 
	$ret = preg_replace("#(^|[\n ]|<a(.*?)>)http://video.google.([a-z]+)/videoplay\?docid=([0-9\-_]+)(</a>)?#im",'<embed style="width:400px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.$3/googleplayer.swf?docId=$4"> </embed><br /><a href="http://video.google.$3/videoplay?docid=$4" target="_blank">http://video.google.$3/videoplay?docid=$4</a>, $ret);
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

a welcome addition ;-) Thanks tvleeuwen
libralion
Forum Newbie
Posts: 2
Joined: Fri Apr 20, 2007 6:08 am

Re: youtube auto-embedding for phpBB

Post by libralion »

Kieran Huggins wrote:I just wrote some code for a phpBB installation (I guess it's a MOD, sort-of) that I thought people could pitch-in to if they find it useful:

This MOD replaces URLs to youTube videos with their embedded counterparts.

Edit 'includes/bbcode.php' and find:

Code: Select all

function make_clickable($text)
{
	$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text);

	// pad it with a space so we can match things at the start of the 1st line.
	$ret = ' ' . $text;
then add:

Code: Select all

// youtube code..
	$ret = preg_replace("#(^|[\n ]|<a(.*?)>)http://(www\.)?youtube\.com/watch\?v=([a-zA-Z0-9\-_]+)(</a>)?#im",' <!-- BEGIN youtube --><object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/$4"></param><embed src="http://www.youtube.com/v/$4" type="application/x-shockwave-flash" width="425" height="350"></embed></object><br /><a href="http://youtube.com/watch?v=$4" target="_blank">http://youtube.com/watch?v=$4</a><!-- END youtube -->', $ret);
This works with plaintext URLs and already linked URLs, and with or without the "www".

If anyone wants to improve this code, my suggestion would be to not embed the video if it's quoted.

Any takers?

Maybe after we're done here one of the mods would drop this in the snippets section?

Cheers,
Kieran
Hello Kieran,

I changed the bbcode.php, but I don't see the videoplayer of youtube on my page.
What am I doing wrong?
http://tina-turner.nl/phpBB2/viewtopic.php?p=324#324

Johanna
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

seems to be working...
libralion
Forum Newbie
Posts: 2
Joined: Fri Apr 20, 2007 6:08 am

Works now

Post by libralion »

Kieran Huggins wrote:seems to be working...
Hello Kieran,

Yes I just had to insert the complete embed code. I thought I only had to write down an url.
Thanks,

Johanna :D
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I just type in the URL... the code above should transform it into the embed code
Post Reply