超链接 Youtube 以嵌入代码 [英] Hyperlink Youtube to Embed Code

查看:31
本文介绍了超链接 Youtube 以嵌入代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Preg_replace 和 preg_match_all 将 Youtube URL 转换为嵌入代码时遇到了一些麻烦.是的,我知道这个话题已经在 stackoverflow 中有所涉及,但并不完全是我想要的.

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.

我可以从一个没有 html 的 url 中获取 ID:

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

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

但我的网址格式如下:

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

有人可以做一些魔术并告诉我检测所有网址的正确表达式,一次获取ID并将所有内容转换为嵌入代码?在此先感谢您!

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!

更新信息:

为了帮助并使其更简洁...

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

我有这个:

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

我想得到这个:

<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!

推荐答案

将 $html 替换为需要解析的 html 字符串.

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;

这篇关于超链接 Youtube 以嵌入代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆