輸入是一個Wikipedia頁面的第一個段落。我想刪除括號和括號之間的任何內容。正則表達式模式以去除括號(和內部的任何括號內)
然而,有時(通常),HTML內容括號內本身含有一個或數個括號,一般在一個鏈路的href=""
。
採取以下:
<p>
The <b>Sarcopterygii</b> or <b>lobe-finned fish</b> (from Greek σαρξ <i>sarx</i>, flesh, and πτερυξ <i>pteryx</i>, fin) – sometimes considered synonymous with <b>Crossopterygii</b> ("fringe-finned fish", from Greek κροσσός <i>krossos</i>, fringe) – constitute a <a href="/wiki/Clade" title="Clade">clade</a> (traditionally a <a href="/wiki/Class_(biology)" title="Class (biology)">class</a> or subclass) of the <a href="/wiki/Osteichthyes" title="Osteichthyes">bony fish</a>, though a strict <a href="/wiki/Cladistic" class="mw-redirect" title="Cladistic">cladistic</a> view includes the terrestrial <a href="/wiki/Vertebrate" title="Vertebrate">vertebrates</a>.
</p>
我想最終的結果是:
<p>
The <b>Sarcopterygii</b> or <b>lobe-finned fish</b> – sometimes considered synonymous with <b>Crossopterygii</b> – constitute a <a href="/wiki/Clade" title="Clade">clade</a> of the <a href="/wiki/Osteichthyes" title="Osteichthyes">bony fish</a>, though a strict <a href="/wiki/Cladistic" class="mw-redirect" title="Cladistic">cladistic</a> view includes the terrestrial <a href="/wiki/Vertebrate" title="Vertebrate">vertebrates</a>.
</p>
但是當我使用下面的preg_replace
模式它不工作,成爲它就會迷茫圓括號內的括號。
public function removeParentheses($content) {
$pattern = '@\(.*?\)@';
$content = preg_replace($pattern, '', $content);
$content = str_replace(' .', '.', $content);
$content = str_replace(' ', ' ', $content);
return $content;
}
其次,我怎麼能離開內部鏈接href=""
和title=""
括號?這些,如果不在文本括號內,則很重要。
正則表達式不能處理遞歸。如果你有一些遞歸模式(括號內括號..)你需要更多的邏輯 - 即寫一個解析器 – Philipp
不要用正則表達式解析HTML。正如@Philipp所說,它無法有效地做到這一點(當然,你可以一起使用一個可行的版本,但我保證你可以通過HTML中的一些不明確的東西來打破它)。使用像[SimpleXML的]一個XML解析器(http://php.net/manual/en/simplexml.examples.php) – ctwheels
你可能要參考https://stackoverflow.com/questions/3577641/how-do-you -parse和工藝-HTML-XML功能於PHP的工具列表,如果試圖解析HTML用PHP – Jeff