Written by davidonzo on 13/09/2008, Filled in OpenSource, Web, Tutorial

xhtml at davidonzo.comBlogs, online journals and in general the 90% of the content management system, use XHTML output as markup language. Both strict and transitional version.

 

Often people choose to embed youtube videos in their websiste. To avoid the annoing cut & paste of the embed code given by youtube, I choose to code a simple script to replace a proprierty tag, whit the embed code.

 

embed

 

This little piece of code, allow me to write a simple line, containing the pseudo tag youtube and a video's permalink inside and show the embeded video without copy and paste the relative code.

 

In the first versione of the script I just used the youtube embed code model. But the code generated using the original youtube code, is NOT HTML valid. So I decide to rewrite it more carefull, and make it XHTML valid.

 

Using PHP, the result is a simple mono-line script.

 


$result = preg_replace('#\[youtube\]http://www.youtube.com/watch\?v=(.*?)\[/youtube\]#i', '<object type="application/x-shockwave-flash" style="width:450px; height:366px;" data="http://www.youtube.com/v/\\1"><param name="movie" value="http://www.youtube.com/v/\\1" /><param name="wmode" value="transparent" /></object>',$testo)

 

Where $testo is the pattern to check.

Did you find interesting this article?
Subscribe my feed to be advised of any new post!
 
.Comments rss
# 1
Btw.. This script isnt run in my blog. How to fix it.. Please bring me example.. Thank...
By Cerita  (Sent 23/09/2008 @ 19:51:44)
# 2
This blog is not wordpress.
This is a self made blog platform :-)

To apply this code to wordpress, you should script a plugin to make change to the_content() function output.
By davidonzo  (Sent 23/09/2008 @ 21:55:16)
# 3
how could i made to invert the process??

I have a string like <object etc etc.... and i want a string like [youtube] etc etc
By v  (Sent 19/11/2008 @ 18:20:57)
# 4
Accordi the embed youtube code you should use something like this

preg_replace('#<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/(.*?)"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/(.*?)" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>#i', '[youtube]http://www.youtube.com/watch?v=//1[/youtube]');


Be advised, I haven't tested it.
By davidonzo  (Sent 20/11/2008 @ 10:40:12)


Comments could be moderated.
If you don't see it immediately published, please avoid to insert it one more time.
Thanks for your patient.