Don't parse HTML via preg_match
, use this PHP class instead:
The DOMDocument class
Example:
hi
H1 title
H2 title
H2 title
";
// a new dom object
$dom = new domDocument('1.0', 'utf-8');
// load the html into the object
$dom->loadHTML($html);
//discard white space
$dom->preserveWhiteSpace = false;
$hTwo= $dom->getElementsByTagName('h2'); // here u use your desired tag
echo $hTwo->item(0)->nodeValue;
//will return "H2 title";
?>
Reference