输入是一个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