【问题标题】:preg_replace() with variable order of src, height and width [duplicate]preg_replace() 具有可变的 src、高度和宽度顺序 [重复]
【发布时间】:2016-01-01 07:27:04
【问题描述】:

我的正则表达式很糟糕,所以我希望有人能提供帮助!我正在尝试查找与文档中嵌入的任何 YouTube 相匹配的任何 iframe 并替换它们,例如以下任何内容:

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

请注意,可能有双引号、单引号、srcheightwidth 属性的顺序可能会有所不同等等。谁能帮忙!

【问题讨论】:

  • 最后一个示例使用' 打开src 属性并使用" 关闭它。这就是你想要的吗?
  • @LinusKleen 好像是复制粘贴错误,我已经修复了。

标签: php regex preg-replace


【解决方案1】:

试试这个:

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

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

【讨论】:

  • 还有办法检查http还是https吗?
  • 这是我的用法,我认为是因为我使用的是 preg_replace 与 preg_match_all:$input = preg_replace('/(&lt;iframe.*?src=[\"\']https:\/\/youtube.com[\"\'].*?\&gt;)(.*?)(\&lt;\/iframe\&gt;)/gm', "test", $input, 1);
  • 嗯,也许,g 搜索所有匹配,m 用于多行,也许你不需要它们!我更新了正则表达式以支持 http 和 https!
  • 看这个,stackoverflow.com/questions/19061037/…,你不需要使用'g'参数! :)
  • 感谢您的帮助 :) 我确实看到了,我正在使用以下内容:$input = '&lt;iframe width="560" height="315" src="https://www.youtube.com/embed/tvrXet3Lx4U" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;'; $input = preg_replace('/(&lt;iframe.*?src=[\"\']https?:\/\/youtube.com[\"\'].*?\&gt;)(.*?)(\&lt;\/iframe\&gt;)/m', "test", $input, 1); 但似乎没有任何改变
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-09
  • 1970-01-01
  • 2017-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多