youtube auto-embedding for phpBB

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

Moderator: General Moderators

youtube auto-embedding for phpBB

Postby Kieran Huggins » Fri Dec 22, 2006 2:46 pm

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:
Syntax: [ Download ] [ Hide ]
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:

Syntax: [ Download ] [ Hide ]
   // 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 6:19 am, edited 1 time in total.
User avatar
Kieran Huggins
DevNet Master
 
Posts: 3635
Joined: Wed Dec 06, 2006 5:14 pm
Location: Toronto, Canada

Postby Ambush Commander » Fri Dec 22, 2006 8:42 pm

If someone linked to the youtube video, they probably want it to stay a link. This should be implemented as a custom BBcode tag.
HTML Purifier for Standards-Compliant HTML Filtering
User avatar
Ambush Commander
DevNet Master
 
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Postby Neno » Sat Dec 30, 2006 9:53 am

Hi,
youtube auto-embedding is perfect for me. Could you write a similar code for google videos auto-embedding?
Thank you
Neno
Forum Newbie
 
Posts: 3
Joined: Sat Dec 30, 2006 9:46 am

Postby Kieran Huggins » Sat Dec 30, 2006 5:13 pm

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)
User avatar
Kieran Huggins
DevNet Master
 
Posts: 3635
Joined: Wed Dec 06, 2006 5:14 pm
Location: Toronto, Canada

Postby Neno » Sat Dec 30, 2006 7:47 pm

:D OK I've tried

Syntax: [ Download ] [ Hide ]
$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... :?
Neno
Forum Newbie
 
Posts: 3
Joined: Sat Dec 30, 2006 9:46 am

Postby Kieran Huggins » Sat Dec 30, 2006 8:06 pm

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

Syntax: [ Download ] [ Hide ]
// 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 6:19 am, edited 1 time in total.
User avatar
Kieran Huggins
DevNet Master
 
Posts: 3635
Joined: Wed Dec 06, 2006 5:14 pm
Location: Toronto, Canada

Postby Neno » Sat Dec 30, 2006 8:26 pm

Great!
Thank you very much
Neno
Forum Newbie
 
Posts: 3
Joined: Sat Dec 30, 2006 9:46 am

Postby thiscatis » Sat Dec 30, 2006 9:33 pm

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
thiscatis? This Cat is A Landmine - 65daysofstatic
thiscatis
Forum Contributor
 
Posts: 430
Joined: Thu Jul 20, 2006 11:00 am

Postby Kieran Huggins » Sun Dec 31, 2006 5:11 am

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 :-)
User avatar
Kieran Huggins
DevNet Master
 
Posts: 3635
Joined: Wed Dec 06, 2006 5:14 pm
Location: Toronto, Canada

Postby tvleeuwen » Sun Mar 25, 2007 8:36 am

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


Syntax: [ Download ] [ Hide ]
// 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);
tvleeuwen
Forum Newbie
 
Posts: 1
Joined: Sun Mar 25, 2007 7:58 am

Postby Kieran Huggins » Mon Mar 26, 2007 2:42 am

a welcome addition ;-) Thanks tvleeuwen
User avatar
Kieran Huggins
DevNet Master
 
Posts: 3635
Joined: Wed Dec 06, 2006 5:14 pm
Location: Toronto, Canada

Re: youtube auto-embedding for phpBB

Postby libralion » Fri Apr 20, 2007 6:15 am

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:
Syntax: [ Download ] [ Hide ]
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:

Syntax: [ Download ] [ Hide ]
// 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
libralion
Forum Newbie
 
Posts: 2
Joined: Fri Apr 20, 2007 6:08 am

Postby Kieran Huggins » Fri Apr 20, 2007 9:15 am

seems to be working...
User avatar
Kieran Huggins
DevNet Master
 
Posts: 3635
Joined: Wed Dec 06, 2006 5:14 pm
Location: Toronto, Canada

Works now

Postby libralion » Fri Apr 20, 2007 9:36 am

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
libralion
Forum Newbie
 
Posts: 2
Joined: Fri Apr 20, 2007 6:08 am

Postby Kieran Huggins » Sat Apr 21, 2007 2:23 am

I just type in the URL... the code above should transform it into the embed code
User avatar
Kieran Huggins
DevNet Master
 
Posts: 3635
Joined: Wed Dec 06, 2006 5:14 pm
Location: Toronto, Canada

Next

Return to Regex

Who is online

Users browsing this forum: No registered users and 1 guest