Hyperlink Youtube to Embed Code

假如想象 提交于 2019-12-12 06:16:50

问题


I've some troubles using Preg_replace and preg_match_all to convert a Youtube URL to embed code. Yes, I know that this topic has already touched in stackoverflow but not exactly like I want.

I can get the ID from a url, without html, with that:

http://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)(\w*)(&(amp;)?[\w\?=]*)?

But I've the url formatted with like this:

<a href="http://www.youtube.com/watch?v=C9KAqhbIZ7o" class="comment-link">http://www.youtube.com/watch?v=C9KAqhbIZ7o</a>

And I want to convert it all to this:

<object width="640" height="505"><param name="movie" value="http://www.youtube.com/v/C9KAqhbIZ7o?fs=1&amp;hl=es_ES&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/C9KAqhbIZ7o?fs=1&amp;hl=es_ES&amp;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object>

Somebody can do some magic and tell me the correct expression to detect all the url, get the ID once and convert all to an embed code? Thank you so much in advance!

Update information:

In order to help and make it a more concise...

I've this:

<p>This is an example of comment</p><strong>Hi bold!</strong><p>Look a youtube url! <a href="http://www.youtube.com/watch?v=C9KAqhbIZ7o" class="comment-link">http://www.youtube.com/watch?v=C9KAqhbIZ7o</a></p>

And I want to get this:

<p>This is an example of comment</p><strong>Hi bold!</strong><p>Look a youtube url! <object width="640" height="505"><param name="movie" value="http://www.youtube.com/v/C9KAqhbIZ7o?fs=1&amp;hl=es_ES&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/C9KAqhbIZ7o?fs=1&amp;hl=es_ES&amp;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object></p>

Thanks all for your help, I really appreciate it!


回答1:


Replace $html with your html string which needs parsing.

$html=<<<HTML
<p>This is an example of comment</p><strong>Hi bold!</strong><p>Look a youtube url! <a href="http://www.youtube.com/watch?v=C9KAqhbIZ7o" class="comment-link">http://www.youtube.com/watch?v=C9KAqhbIZ7o</a></p>

HTML;


$regex="/v\=([\-\w]+)/";

preg_match_all($regex,$html,$out);

$out[1]=array_unique($out[1]);

foreach($out[1] as $o){

        $reg="/(<a).*(youtube.com).*($o).*(\/a>)/";

        $embed= <<<HTML
        <object width="640" height="505"><param name="movie" value="http://www.youtube.com/v/$o=1&amp;hl=es_ES&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/$o?fs=1&amp;hl=es_ES&amp;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object>
HTML;

        $html=preg_replace($reg,$embed, $html);

}

echo $html;



回答2:


I use this code

        // url of video
    $url = $row['url'];
    $id=0;
    // we get the unique video id from the url by matching the pattern
    preg_match("/v=([^&]+)/i", $url, $matches);
    if(isset($matches[1])) $id = $matches[1];
    if(!$id) {
        $matches = explode('/', $url);
        $id = $matches[count($matches)-1];
    }
    // this is your template for generating embed codes
    $code = '<div id="img_wrapper"><object width="640" height="458"><param name="movie" value="http://www.youtube.com/v/{id}&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/{id}&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></div>';

    // we replace each {id} with the actual ID of the video to get embed code for this particular video
    $code = str_replace('{id}', $id, $code);

    echo $code;


来源:https://stackoverflow.com/questions/4713311/hyperlink-youtube-to-embed-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!