Preg match text in php between html tags

David Willis picture David Willis · Oct 19, 2009 · Viewed 96.4k times · Source

Hello I would like to use preg_match in PHP to parse the "Desired text" out of the following from a html document

<p class="review"> Desired text </p>

Ordinarily I would use simple_html_dom for such things but on this occasion it cannot be used (the above element doesn't appear in every desired div tag so I'm forced to use this approach to keep track of exactly when it doesn't appear and then adjust my array from simple_html_dom accordingly).

Anyway, this would solve my problem.

Thanks so much.

Answer

serg picture serg · Oct 19, 2009
preg_match("'<p class=\"review\">(.*?)</p>'si", $source, $match);
if($match) echo "result=".$match[1];