2015-10-04 82 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