Multiline preg_match

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

According to the manual, the default for preg_match is to treat the subject
string as consisting of a single "line" of characters (even if it actually
contains several newlines).

I want to match the "string" below to extract everyting from <strong> to
</div> (not inclusive).
However my attempt at preg_match('/<strong(.*)/',$fc,$match) returns only
<strong>RCR002</strong><br />

Any suggestions welcome.

<html xmlns=" ">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>

<div align="center"><img
src="../../../images/stories/rhinestone_stock/crowns/RCR002.gif" width="200"
height="200" />
  <br />
  <strong>RCR002</strong><br />
  2mm &amp; 3mm<br />
  ~5.25&rdquo;W x 3.25&rdquo;H</div>

Re: Multiline preg_match

Quoted text here. Click to load it


$arr = explode("\n", $string); // Where $string is your block of text
foreach($arr as $key => $value) {
   preg_match('pattern', $value, $matches[$key]);

Re: Multiline preg_match

Mike Camden wrote:
Quoted text here. Click to load it

I have managed to do it using str_replace to change all the newlines to
However I'd really like to understand why preg_match does not behave as the
manual suggests.

Re: Multiline preg_match

On Wed, 16 Apr 2008 23:04:14 +0200, Paul Lautman  =

Quoted text here. Click to load it

 From the preg match portion of the manual:
    . = match any character _except_newline_ (by default)

    If this modifier is set, a dot metacharacter in the pattern
    matches all characters, including newlines. Without it,
    newlines are excluded.

In conclusion (giving you the bonus 'untill </div> non-inclusive'):

Which leaves me to say that while I'm a fan of regexes, I've given up  =

using them on HTML, because a parser does a far more reliable, clearer, =

and most important more robust job.
-- =

Rik Wasmus

Re: Multiline preg_match

Quoted text here. Click to load it

Thanks Rik.

In this case I don't want to parse the HTML. I want to extract a
particular chunk from many files.

Site Timeline