PHP 正则表达式 &YouTube [英] PHP Regex & YouTube

查看:34
本文介绍了PHP 正则表达式 &YouTube的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写临时 PHP 脚本来更新我的 vBulletin 论坛的 MySQL 数据库.

I am writing temporary PHP script to update MySQL database of my vBulletin forum.

这就是它的作用.它会查找具有 [youtube][/youtube] 代码的任何条目.然后它必须用指向 youtube 视频的链接替换该代码.

Here's what it does. It finds any entry that has a [youtube][/youtube] code. And then it has to replace that code with a link to the youtube video instead.

所以,这是我必须采取的一个例子:

So, here is an example of what I have to take:

$string = <<<END
Hi everyone! Check out this video that I just found on YouTube!

[youtube]<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/Md1E_Rg4MGQ&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/Md1E_Rg4MGQ&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>[/youtube]
END;

我必须让它看起来像这样:

And I have to make that look like this instead:

[URL=http://www.youtube.com/watch/?v=Md1E_Rg4MGQ]http://www.youtube.com/watch/?v=Md1E_Rg4MGQ[/URL]

我在使用 Regex 时头疼.我对 Regex 的经验不足,无法弄清楚该怎么做.

I'm getting a headache working with the Regex. I don't have enough experience with Regex to figure out what to do.

它必须看起来像这样:

$string = preg_replace("#\[youtube\]?????\[/youtube\]#i", "[URL=http://www.youtube.com/watch?v=$1]http://www.youtube.com/watch?v=$1[/URL]", $string);

请帮忙!^_^

推荐答案

可能是这样的?

$string = preg_replace('#\[youtube\].*?name="movie" value="(.*?)".*?\[/youtube\]#i', "[URL=$1]$1[/URL]", $string);

请注意,这受到限制,因为它需要一个字符串 name="movie" value="????????????????" 精确格式 - 从技术上讲,还有其他具有相同含义但顺序不同的有效 HTML 构造等.理想的解决方案是使用某种 DOM 解析器来获取电影属性的值,但如果您知道人们将始终使用该确切格式(即,如果那是从 youtube 复制/粘贴总是如此),那么正则表达式就足够了.

Note that this is limited in that it expects a string name="movie" value="????????????????" format precisely - technically, there are other valid HTML constructs with the same meaning but different order, etc. The ideal solution would be to use some sort of DOM parser to grab the value of the movie attribute, but if you know people will always be using that exact format (i.e. if that's copy the copy/paste from youtube always is or the like) then regex can suffice.

这篇关于PHP 正则表达式 &amp;YouTube的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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