tags:

views:

1931

answers:

3

I want to get all only youtube video ID from html code

look the (or multiple) object/embed code for youtube video

// html from database

    <p>loremm ipsum dolor sit amet enot
    <a href="link" attribute=""blah blah blah">anchor link</a>
    </p>

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

    <image src="path/to/image.ext" >
    <p>lorem ipsum dolor sit amet... blah</p>
    <p>lorem ipsum dolor sit amet... blah</p>

    <object width="425" height="344">
        <param name="movie" value="http://www.youtube.com/v/Ou5eVl5eqtg&amp;hl=es_ES&amp;fs=1&amp;"&gt;&lt;/param&gt;
        <param name="allowFullScreen" value="true"></param>
        <param name="allowscriptaccess" value="always"></param>
        <embed src="http://www.youtube.com/v/Ou5eVl5eqtg&amp;hl=es_ES&amp;fs=1&amp;"
    type="application/x-shockwave-flash"
    allowscriptaccess="always"
    allowfullscreen="true"
    width="425"
    height="344">
    </embed>
        </object>
<p>blah</p>
blah<br/>
blah<br/>
blah<br/>
A: 

I might get scolded for using a regex to parse html but given the circumstances maybe it's the best way to do it?

preg_match('~/v/([0-9a-z_]+)~i', $code, $matches);
echo $matches[1];

assuming the valid characters for a youtube video id are 0-9a-z_

Galen
it works! , but there is a way to do this safely, forcing only for youtube vids?
asumaran
add youtube.com before the /v/
Galen
+1  A: 

Brazenly stolen from htmlpurifier's youtube plugin:

preg_match('#<object[^>]+>.+?http://www.youtube.com/v/([A-Za-z0-9\-_]+).+?&lt;/object&gt;#s', $markup, $matches);
var_dump($matches[1]);
Frank Farmer
+4  A: 

There are generally two formats for YouTube video urls:

http://www.youtube.com/v/[videoid]
http://www.youtube.com/watch?v=[videoid]

The "www.youtube.com" can be replaced by "www.youtube.co.uk", or other country codes, but as far as I've been able to determine, the video ids are the same regardless of the domain name.

The video id is an 11-character string that uses base-64 encoding.

Assuming you have code that will parse urls from an HTML document, you can determine if it's a YouTube video url and get the video id by using this regex (written in C#, but should be easily converted to php or anything else):

"^http://(?&lt;domain&gt;([^./]+\\.)*youtube\\.com)(/v/|/watch\\?v=)(?&lt;videoId&gt;[A-Za-z0-9_-]{11})"

This particular regex is specific to youtube.com. Making it understand all the different country codes (youtube.co.uk, youtube.pl, youtube.it, etc.) is somewhat more involved.

Jim Mischel
I need this in php :(
asumaran
I think you can figure out how to translate it.
Jim Mischel