Preg_replace 对于特定的 iframe

Preg_replace for a specific iframe

正在编写一个快速插件以从电子邮件和 Feed 中删除 Amazon 员工 links 以不违反服务条款。除了 iframe 之外,一切都解决了。

典型的 iframe 关联 link 如下所示

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

这个正则表达式

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

拾取除结束 iframe 标记之外的所有内容 我试过

的变体
/(<iframe )(.*)(amazon.adsystem.com).*(</iframe>)/

但无法正常工作。有什么想法吗?

我们可能想在此处使用类似于以下的表达式传递换行符:

(<iframe)([\s\S]*?)(amazon.adsystem.com)([\s\S]*?)(<\/iframe>)

Demo

测试

$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>"
  }