2015-10-04 108 views
-1

我很可怕的正则表达式,所以我希望有人可以帮助!我试图找到匹配任何YouTube的文档中嵌入任何I帧和替换它们,例如以下任何一种:preg_replace()与可变顺序的src,高度和宽度

<iframe src="http://youtube.com" width="100" height="200"></iframe> 
<iframe src='http://youtube.com' width="100" height="200"></iframe> 
<iframe width="100" src='http://youtube.com' width="100"></iframe> 
etc. 

注意,有可能是双引号,单引号的src的顺序,heightwidth属性可能会有所不同等任何人都可以帮助!

+0

最后一个例子使用'''打开'src'属性和'“'关闭它是不是你想要的 –

+0

@LinusKleen似乎?像复制粘贴错误,我已修复它 – Xufox

回答

0

试试这个:

/(<iframe.*?src=([\"\'])https?:\/\/w{0,3}.?youtube.com.*?(\2).*?\>)(.*?)(\<\/iframe\>)/gm 

https://regex101.com/r/fF4vA6/6

+0

是否还有一种方法来检查http或https? – user2988034

+0

这是我的用法,我认为它是因为我正在使用preg_replace vs preg_match_all: '$ input = preg_replace ('/()(。*?)(\ <\/iframe\>)/ gm',“test”,$ input,1);' – user2988034

+0

mmm,可能是搜索所有匹配的g,而m是多行的,也许你不需要它们! 我更新了正则表达式来支持http和https! – mayo