tags:

views:

39

answers:

1

I have a site based on php/mySQL where publishers had inserted a lot of youtube videos in a specific database column copiyng/pasting youtube embed codes. There is a wide variance of the codes reflecting the variations google made during time. Different sizes of the player window is the main variation. Now I need to uniform all the windows, but I don't know where to start. What I need to reach is something like a regexp to eliminate all the not necessary code from the various entries (i.e. convert all embed codes in their corresponding youtube url?) and then create a script to generate the player code runtime... Some help to look where to start? Thank you!

+1  A: 

New Complete Solution:

<?php
//your bad code which you want to sanitize
$BadCode = '<object width="640" height="385">
<param name="movie" value="http://www.youtube.com/v/TvJsRX9SKUo?fs=1&amp;hl=it_IT"&gt;&lt;/param&gt;
<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube.com/v/TvJsRX9SKUo?fs=1&amp;hl=it_IT" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed>
</object>';


//Get Youtube Video ID
preg_match('#(?<=youtube\.com/v/)\w+#', $BadCode, $matches);
$VideoID = $matches[0];


//New Correct and consistent embed code template. Create whatever consistent template you want. Then all your videos will follow this template. Change the place where video-id appears as [VIDEOID]
$template = '<object type="application/x-shockwave-flash" style="width:450px; height:366px;" data="http://www.youtube.com/v/[VIDEOID]?showsearch=0&amp;amp;fs=1"&gt;
<param name="movie" value="http://www.youtube.com/v/[VIDEOID]?showsearch=0&amp;amp;fs=1" /><param name="allowFullScreen" value="true" />
</object>';

//Insert our VideoID into this template.
$GoodCode = str_replace('[VIDEOID]', $VideoID, $template);

//Display code for testing
echo $GoodCode;

//Update your database table (please change variables yourself)
//mysql_query("update tablename set YoutubeCode = '".$GoodCode."' where ID={$ID}");
?>

Old Summarized Answer:

[Regex for extracting video ID]. After getting video ID, you have the video ID. Use this ID to generate a clean consistent code for all your videos. Then, update the database.

shamittomar
TheDarkMist
@TheDarkMist , I have refined my answer. Look at it now.
shamittomar