Jump to content

[SOLVED] How to Strip Tags from YouTube Embed Code


hoopplaya4

Recommended Posts

Hi All,

 

I'm trying to write a PHP script to strip the embed code of a YouTube Video.

 

For example, with a YouTube embed code like this:

 

<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/5P6UU6m3cqk&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/5P6UU6m3cqk&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>

 

I'd like to strip it down to just this:

 

5P6UU6m3cqk

 

I know that I would need to use "str_replace" or perhaps "strip_tags" but I'm not sure how to go about doing so.  Any help would be very much appreciated!

So you want to get the video ID out? Try this:

 

$str = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/5P6UU6m3cqk&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/5P6UU6m3cqk&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>';

preg_match("#http://www.youtube.com/v/([\d\w]+)&.*#i", $str, $match);
echo $match[1];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.