0
votes

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;
    }
2
Can you show us what the source text looks like? And then what you're searching for?Keng

2 Answers

2
votes

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.

1
votes

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