超链接 Youtube 以嵌入代码
我在使用 Preg_replace 和 preg_match_all 将 Youtube URL 转换为嵌入代码时遇到一些麻烦。是的,我知道这个话题已经在 stackoverflow 中涉及到了,但并不完全像我想要的那样。
我可以从一个没有 html 的 url 中获取 ID:
http://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)(\w*)(&(amp;)?[\w\?=]*)?
但我的 url 格式如下:
<a href="http://www.youtube.com/watch?v=C9KAqhbIZ7o" class="comment-link">http://www.youtube.com/watch?v=C9KAqhbIZ7o</a>
我想将其全部转换为:
<object width="640" height="505"><param name="movie" value="http://www.youtube.com/v/C9KAqhbIZ7o?fs=1&hl=es_ES&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&hl=es_ES&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object>
有人可以施展一些魔法,告诉我检测所有内容的正确表达式url,获取一次 ID 并将其全部转换为嵌入代码?提前非常感谢!
更新信息:
为了帮助并使其更加简洁...
我有这个:
<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&hl=es_ES&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&hl=es_ES&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object></p>
谢谢大家的帮助,我真的很感激!
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&hl=es_ES&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&hl=es_ES&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&hl=es_ES&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&hl=es_ES&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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我用这个代码
I use this code
将 $html 替换为需要解析的 html 字符串。
Replace $html with your html string which needs parsing.