-2

Writing a quick plugin to strip Amazon associate links out of emails and feed to not brake the terms ofservice. Worked it all out apart from the iframe.

A typical iframe associate link looks like this

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

This regexp

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

picks up everything except the closing iframe tag I have tried variations of

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

But can't get it to work. Any ideas?

andymoyle
  • 428
  • 4
  • 12
  • How is your ~( – andymoyle Jun 20 '19 at 19:49
  • I see, here is what I meant: https://3v4l.org/79CF6. I thought the literal string is part of a URL. Still, the literal dot must be escaped, and your only trouble is that `.` does not match a newline by default and how to make it work across lines is a very well known issue, thus, close as a duplicate. – Wiktor Stribiżew Jun 20 '19 at 22:03
  • Now I understand - new to regEx and wasn't aware the newline was the problem. Thanks – andymoyle Jun 21 '19 at 06:21

1 Answers1

0

We might want to pass newlines here with an expression similar to:

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

Demo

Test

$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);

Output

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>"
  }
Emma
  • 1
  • 9
  • 28
  • 53
  • 3
    Please stop reopening evident duplicate questions. They have no added value for the site. At least, please leave comments why you think this or that question is not a dupe. – Wiktor Stribiżew Jun 20 '19 at 16:59
  • 1
    Please also read in your demo link (and Wiktor's comment under the question) about why your pattern is not something that people should be copy-pasting. – mickmackusa Jun 20 '19 at 19:32