Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match content of HTML body in PHP

I need a regex in php for matching contents between tags of an element, e.g. <body> and </body> with the perl compatible preg_match.

So far I tried with:

// $content is a string with html content

preg_match("/<body(.|\r\n)*\/body>/", $content, $matches);

print_r($matches);

…but the printout is an empty array.

like image 272
Spoike Avatar asked Aug 12 '09 08:08

Spoike


1 Answers

You simply have to add the s modifier to have the dot match all characters, including new lines :

preg_match("/<body.*\/body>/s", $content, $matches);

as explained in the doc : http://nl2.php.net/manual/en/reference.pcre.pattern.modifiers.php

like image 142
Wookai Avatar answered Oct 02 '22 00:10

Wookai