Letseatlunch
Letseatlunch

Reputation: 2487

Getting youtube embed video code from a youtube link?

Is there a regex to get youtube embed code from youtube video page links?

I want literaly the opposite of this question: Getting youtube links from embedded youtube video on pages?

EDIT: i need code something like the following:

    public static String GetYoutubeEmbedCode(String YoutubeLink)
    {
        String SourceLink = /* Something involving YoutubeLink*/;
        String YoutubeEmbedCode = "<object width=\"640\" height=\"385\"><param name=\"movie\" value=\"" + SourceLink + "\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"" + SourceLink + "\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"640\" height=\"385\"></embed></object>";
        return YoutubeEmbedCode;
    }

Upvotes: 0

Views: 4316

Answers (2)

guigouz
guigouz

Reputation: 1211

There's a new and easier way to embed youtube videos described on http://apiblog.youtube.com/2010/07/new-way-to-embed-youtube-videos.html

It also supports the HTML5 player if the user has chosen to use it.

Upvotes: 2

Thorbear
Thorbear

Reputation: 2333

Basically you want something like:

$subject = 'http://www.youtube.com/watch?v=Hy4HAPu7lsc'; // the link
$pattern = '%http://www\.youtube\.com/watch\?v=([A-Za-z0-9]+)%';
$replacement = '<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/$1?fs=1&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/$1?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>';
preg_replace($pattern, $replacement, $subject);

Upvotes: 1

Related Questions