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!

Link to comment
Share on other sites

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];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.