2014-02-09 137 views
0

我有类似:PHP正则表达式:在span.class删除括号内的文字

<span class="product-title">title (description)</span> 

,我想用PHP括号和所有span.product-title的内容删除,是这样的:

<span class="product-title">title</span> 

我成功删除了文本和括号(/\s\([^)]+\)/),但我只想匹配span.product-title中的内容。
我该怎么做?
在此先感谢。 :)

+2

不要在html上使用正则表达式。使用DOM隔离该跨度,提取其文本内容,然后处理JUST该文本字符串 –

+1

要扩展@ MarcB的评论,签出['DOMDocument'](http://php.net/manual/en/class.domdocument。 PHP)和['XPath'](http://us2.php.net/manual/en/simplexmlelement.xpath.php)。用于检索特定类的XPath查询将是// // span [@ class =“product-title”]'。 – Sam

回答

1

所有有效(和可取的)建议。但是,如果你只是寻找一个快速&肮脏的正则表达式的修正,这应该这样做:

/(<span class="product-title">.+?)\s*(?=\().+?\)(<\/span>)/i 

与基置换匹配$ 1,$ 2 & $ 4