Get Youtube Video ID from html code with PHP

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&hl=es_ES&fs=1&"></param>
        <param name="allowFullScreen" value="true"></param>
        <param name="allowscriptaccess" value="always"></param>
        <embed src="http://www.youtube.com/v/Ou5eVl5eqtg&hl=es_ES&fs=1&"
    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&hl=es_ES&fs=1&"></param>
        <param name="allowFullScreen" value="true"></param>
        <param name="allowscriptaccess" value="always"></param>
        <embed src="http://www.youtube.com/v/Ou5eVl5eqtg&hl=es_ES&fs=1&"
    type="application/x-shockwave-flash"
    allowscriptaccess="always"
    allowfullscreen="true"
    width="425"
    height="344">
    </embed>
        </object>
<p>blah</p>
blah<br/>
blah<br/>
blah<br/>

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://(?<domain>([^./]+\\.)*youtube\\.com)(/v/|/watch\\?v=)(?<videoId>[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.


Actually, to completely capture all options, I found that WebFlakeStudio's solution is the best, with the following addition, to capture all 3 forms of *cough*client stupidity*cough*

(PHP)

preg_match('#(\.be/|/embed/|/v/|/watch\?v=)([A-Za-z0-9_-]{5,11})#', $YoutubeCode, $matches);
if(isset($matches[2]) && $matches[2] != ''){
     $YoutubeCode = $matches[2];
}

I added the /embed, this should capture all. The Object, the URL and the Embed-option.