views:

338

answers:

2

Hi, I need to retrieve content of <p> tag with given class. Class could be simplecomment or comment ...

So I wrote the following code

preg_match("|(<p class=\"(simple)?comment(.*)?\">)(.*)<\/p>|ism", $fcon, $desc);

Unfortunately, it returns nothing. However if I remove tag-ending part (<\/p>) it works somehow, returing the string which is too long (from tag start to the end of the document) ...

What is wrong with my regular expression?

A: 
Blixt
+2  A: 

Try using a dom parser like http://simplehtmldom.sourceforge.net/

If I read the example code on simplehtmldom's homepage correctly you could do something like this:

$html->find('div.simplecomment', 0)->innertext = '';
bjelli
Suggested third party alternatives to [SimpleHtmlDom](http://simplehtmldom.sourceforge.net/) that actually use [DOM](http://php.net/manual/en/book.dom.php) instead of String Parsing: [phpQuery](http://code.google.com/p/phpquery/), [Zend_Dom](http://framework.zend.com/manual/en/zend.dom.html), [QueryPath](http://querypath.org/) and [FluentDom](http://www.fluentdom.org).
Gordon