Regex to match content of HTML body in PHP

10,962

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

Share:
10,962
Troj
Author by

Troj

I'm a jack of most trades residing in Sweden and usually involved with full-stack web development technologies. I work for tretton37 as a contractor, my list of clients includes among others Sony and IKEA. I dabble in open source software and have many projects in my Github repository and my Bitbucket repository, among many: RefluxJS - Library for uni-directional data flows, inspired by Facebook's Flux In the little free time that I have, all kinds of stuff happen such as drawing pretty pictures, perform ball juggling, play a guitar, hack on games, and solve a Rubik's cube.

Updated on June 14, 2022

Comments

  • Troj
    Troj almost 2 years

    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.

  • Wookai
    Wookai almost 15 years
    Setting the -m flag is not sufficient, as it only changes the behavior of the ^ and $ operators.