2

I have a WordPress plugin that retrieves RSS feeds automatically, some of RSS feeds injects unwanted Ads on the following format:

src="http://rss.feedsportal.com/c/669/f/9809/s/3b7b71e8/sc/5/mf.gif" border="0" /><br clear='all'/><br /><br /><a href="http://da.feedsportal.com/r/199108411625/u/49/f/9809/c/669/s/3b7b71e8/sc/5/rc/1/rc.htm" rel="nofollow"><img src="http://da.feedsportal.com/r/199108411625/u/49/f/9809/c/669/s/3b7b71e8/sc/5/rc/1/rc.img" border="0" /></a><br /><a href="http://da.feedsportal.com/r/199108411625/u/49/f/9809/c/669/s/3b7b71e8/sc/5/rc/2/rc.htm" rel="nofollow"><img src="http://da.feedsportal.com/r/199108411625/u/49/f/9809/c/669/s/3b7b71e8/sc/5/rc/2/rc.img" border="0" /></a><br /><a href="http://da.feedsportal.com/r/199108411625/u/49/f/9809/c/669/s/3b7b71e8/sc/5/rc/3/rc.htm" rel="nofollow"><img src="http://da.feedsportal.com/r/199108411625/u/49/f/9809/c/669/s/3b7b71e8/sc/5/rc/3/rc.img" border="0" /></a><br /><br /><a href="http://da.feedsportal.com/r/199108411625/u/49/f/9809/c/669/s/3b7b71e8/sc/5/a2.htm"><img src="http://da.feedsportal.com/r/199108411625/u/49/f/9809/c/669/s/3b7b71e8/sc/5/a2.img" border="0" /></a><img width="1" height="1" src="http://pi.feedsportal.com/r/199108411625/u/49/f/9809/c/669/s/3b7b71e8/sc/5/a2t.img" border="0" />

The plugin has a Regular Expression Search and Replace tool. Now I want to find all strings/lines of code that contain *feedsportal.com and replace with with null or empty values. What should be this code to be added in Search and what in Replace?

I've also another issue that all post images imported are aligned left while I need to align all images in the post to center while keeping text/paragraph formatting intact?

CODERx86
  • 25
  • 1
  • 7
  • What did you research or try so far? (Regexes aren't magic black box codes that are impossible to figure out). And if the ads are part of the RSS feeds, wouldn't it be fair to assume they're there to finance their publisher? – mario Jul 18 '14 at 22:09
  • @mario Ads are publisher's property and they're free to display them on their own websites and about Regexes I'm newbie to them :) Any guides? – CODERx86 Jul 18 '14 at 22:15
  • * See also [Open source RegexBuddy alternatives](http://stackoverflow.com/questions/89718/is-there) and [Online regex testing](http://stackoverflow.com/questions/32282/regex-testing) for some helpful tools, or [RegExp.info](http://regular-expressions.info/) for a nicer tutorial. - You may want to probe for the constant string part enclosed by `[^<>]+`.. – mario Jul 18 '14 at 22:16

1 Answers1

2

Can't advise on the legal issues, but on the regex, here is how to empty these strings:

$replaced = preg_replace('/"\K[^"]*?feedsportal.*?(?=")/', '', $yourstring);

See the regex demo.

Explanation

  • " matches the opening quote
  • The \K tells the engine to drop what was matched so far from the final match it returns
  • [^"]*? lazily matches any chars that are not a quote up to...
  • feedsportal
  • .*? lazily matches any chars up to...
  • a point where the lookahead (?=") can assert that what follows is a closing quote
  • we replace with the empty string

Reference

zx81
  • 41,100
  • 9
  • 89
  • 105
  • FYI added demo and explanation, let me know if you have questions. :) – zx81 Jul 19 '14 at 00:37
  • Thanks but this script seems to be a line that do search/replace in one line, the plugin I'm using has a Search box and Replace box. The plugin site says it uses PCRE syntax. Search for – The search term or regular expression to find. Replace with – The expression to use for replacing the search term. Note that the Search and Replace feature uses Regular Expressions so you must escaoe any special characters in your search pattern with a slash (\) character. The special characters that need escaping are \^.$|()[] So since I'm not sure the code is ok, what to type in search and replace? – CODERx86 Jul 19 '14 at 01:31
  • The regex I gave you is PCRE. Paste this in the search: `"\K[^"]*?feedsportal.*?(?=")` and an empty string in the replace. – zx81 Jul 21 '14 at 23:31