views:

689

answers:

2

I want to take the youtube embed code that youtube provides and use a regex to convert it to valid FBML code i.e. use the fb:swf tag. Has anyone done something like this? So far the regex I've come up with is :

preg_replace('/<object(.*)<\/object>/i', "Whatever I need here...", $str);

I know its lame but its my first try.

Thanks for the help in advance.

PS: Sample of the Youtube embed code

<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/NWHfY_lvKIQ&amp;hl=en_GB&amp;fs=1&amp;"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/NWHfY_lvKIQ&amp;hl=en_GB&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>
A: 

hi there,

had to do this just the other day, here's a function that i wrote to get the clip id's in a string/input and put them into an array (which you can the insert into your embed object):

preg_match_all('/\/vi?\/([A-Za-z0-9\+_-]+)/i', $str_containing_yt_ids, $yt_ids);

then

print_r($yt_ids);

and if you need to dynamically get titles:

function get_yt_title($clip_id){    
    $feedURL = 'http://gdata.youtube.com/feeds/api/videos/' . $clip_id;
    $entry = simplexml_load_file($feedURL); 
    $video= new stdClass;
    $media = $entry->children('http://search.yahoo.com/mrss/');
    $video->title = ucwords(strtolower($media->group->title));
    return $video->title;       
}
Haroldo
+1  A: 

As far as i've figured, you want to gather the video URL from the embed code and then you do your thing and get whatever you want with it.

Assuming that there would only be two URLs in the whole embed code (source and value) and that there are no quotes in URL's (at least not youtube's) the regex I'd use to get the URL is:

[^(src=")]http.*[^"]

Explanation: http that isn't precceeded by 'src="' is got until there is a quote.

If it's not right, don't downvote it, I've just started learning regex yesterday =)

MarceloRamires