简体   繁体   中英

Using Regular Expressions in WordPress

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM