Php 针对特定iframe的Preg_替换

Php 针对特定iframe的Preg_替换,php,preg-replace,Php,Preg Replace,编写一个快速插件,将Amazon associate的链接从电子邮件和feed中删除,以避免破坏服务条款。除了iframe之外,所有的一切都解决了 典型的iframe关联链接如下所示 <iframe style="width:120px;height:240px;" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="//ws-eu.amazon-adsystem.com/widgets/q?Servi

编写一个快速插件,将Amazon associate的链接从电子邮件和feed中删除,以避免破坏服务条款。除了iframe之外,所有的一切都解决了

典型的iframe关联链接如下所示

 <iframe style="width:120px;height:240px;" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="//ws-eu.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&OneJS=1&Operation=GetAdHtml&MarketPlace=GB&source=ac&ref=qf_sp_asin_til&ad_type=product_link&tracking_id=keepchickens-21&marketplace=amazon&region=GB&placement=1473689309&asins=1473689309&linkId=2ee7072c3050c40cded9c2ecdb317332&show_border=true&link_opens_in_new_window=true&price_color=333333&title_color=0066c0&bg_color=ffffff">
</iframe> 
这个regexp

/(<iframe )(.*)(amazon.adsystem.com).*/
拾取除结束iframe标记之外的所有内容 我尝试过不同的方法

/(<iframe )(.*)(amazon.adsystem.com).*(</iframe>)/

但无法让它工作。有什么想法吗?

我们可能想在这里传递一个类似于以下表达式的换行符:

(<iframe)([\s\S]*?)(amazon.adsystem.com)([\s\S]*?)(<\/iframe>)
测验 输出
请停止重新打开明显重复的问题。它们对站点没有附加值。至少,请留下评论,为什么你认为这个或那个问题不是一个重复。还请阅读演示链接和Wiktor在问题下的评论,关于为什么你的模式不应该被复制粘贴。你的~我明白了,这是我的意思:。我认为文本字符串是URL的一部分。不过,文字点必须转义,而您唯一的问题是。默认情况下,不匹配换行符,如何使其跨行工作是一个众所周知的问题,因此,非常接近于重复。现在我明白了-对regEx来说是新手,不知道换行符是问题所在。谢谢
$re = '/(<iframe)([\s\S]*?)(amazon.adsystem.com)([\s\S]*?)(<\/iframe>)/m';
$str = '<iframe style="width:120px;height:240px;" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="//ws-eu.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&OneJS=1&Operation=GetAdHtml&MarketPlace=GB&source=ac&ref=qf_sp_asin_til&ad_type=product_link&tracking_id=keepchickens-21&marketplace=amazon&region=GB&placement=1473689309&asins=1473689309&linkId=2ee7072c3050c40cded9c2ecdb317332&show_border=true&link_opens_in_new_window=true&price_color=333333&title_color=0066c0&bg_color=ffffff">
</iframe> some other text <iframe style="width:120px;height:240px;" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="//ws-eu.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&OneJS=1&Operation=GetAdHtml&MarketPlace=GB&source=ac&ref=qf_sp_asin_til&ad_type=product_link&tracking_id=keepchickens-21&marketplace=amazon&region=GB&placement=1473689309&asins=1473689309&linkId=2ee7072c3050c40cded9c2ecdb317332&show_border=true&link_opens_in_new_window=true&price_color=333333&title_color=0066c0&bg_color=ffffff">
</iframe> ';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);
array(2) {
  [0]=>
  array(6) {
    [0]=>
    string(513) "<iframe style="width:120px;height:240px;" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="//ws-eu.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&OneJS=1&Operation=GetAdHtml&MarketPlace=GB&source=ac&ref=qf_sp_asin_til&ad_type=product_link&tracking_id=keepchickens-21&marketplace=amazon&region=GB&placement=1473689309&asins=1473689309&linkId=2ee7072c3050c40cded9c2ecdb317332&show_border=true&link_opens_in_new_window=true&price_color=333333&title_color=0066c0&bg_color=ffffff">
</iframe>"
    [1]=>
    string(7) "<iframe"
    [2]=>
    string(112) " style="width:120px;height:240px;" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="//ws-eu."
    [3]=>
    string(19) "amazon-adsystem.com"
    [4]=>
    string(366) "/widgets/q?ServiceVersion=20070822&OneJS=1&Operation=GetAdHtml&MarketPlace=GB&source=ac&ref=qf_sp_asin_til&ad_type=product_link&tracking_id=keepchickens-21&marketplace=amazon&region=GB&placement=1473689309&asins=1473689309&linkId=2ee7072c3050c40cded9c2ecdb317332&show_border=true&link_opens_in_new_window=true&price_color=333333&title_color=0066c0&bg_color=ffffff">
"
    [5]=>
    string(9) "</iframe>"
  }
  [1]=>
  array(6) {
    [0]=>
    string(513) "<iframe style="width:120px;height:240px;" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="//ws-eu.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&OneJS=1&Operation=GetAdHtml&MarketPlace=GB&source=ac&ref=qf_sp_asin_til&ad_type=product_link&tracking_id=keepchickens-21&marketplace=amazon&region=GB&placement=1473689309&asins=1473689309&linkId=2ee7072c3050c40cded9c2ecdb317332&show_border=true&link_opens_in_new_window=true&price_color=333333&title_color=0066c0&bg_color=ffffff">
</iframe>"
    [1]=>
    string(7) "<iframe"
    [2]=>
    string(112) " style="width:120px;height:240px;" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="//ws-eu."
    [3]=>
    string(19) "amazon-adsystem.com"
    [4]=>
    string(366) "/widgets/q?ServiceVersion=20070822&OneJS=1&Operation=GetAdHtml&MarketPlace=GB&source=ac&ref=qf_sp_asin_til&ad_type=product_link&tracking_id=keepchickens-21&marketplace=amazon&region=GB&placement=1473689309&asins=1473689309&linkId=2ee7072c3050c40cded9c2ecdb317332&show_border=true&link_opens_in_new_window=true&price_color=333333&title_color=0066c0&bg_color=ffffff">
"
    [5]=>
    string(9) "</iframe>"
  }