簡體   English   中英

在WordPress中使用正則表達式

[英]Using Regular Expressions in WordPress

我有一個WordPress插件,可以自動檢索RSS提要,某些RSS提要以以下格式注入不需要的廣告:

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

該插件有一個正則表達式搜索和替換工具。 現在,我想查找包含* feedsportal.com的所有字符串/代碼行,並替換為null或空值。 此代碼應在搜索中添加什么代碼,在替換中應添加什么代碼?

我還有另一個問題,就是所有導入的帖子圖像都左對齊,而我需要將帖子中的所有圖像對齊到中心,同時保持文本/段落格式的完整性?

無法就法律問題提供建議,但是在正則表達式上,這是如何清空這些字符串的方法:

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

參見regex演示

說明

  • "相匹配的開引號
  • \\K告訴引擎放棄與最終匹配相距甚遠的匹配,它返回
  • [^"]*?懶惰地匹配所有不超過引號的字符...
  • feedsportal
  • .*? 懶惰地匹配任何字符直到...
  • 先行(?=")可以斷言其后的內容是結束語
  • 我們用空字符串替換

參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM