tags:

views:

149

answers:

3

I am trying to get a regular expression to work, but not having a whole lot of luck.

the source file I am reading(poorly formatted, but nothing I can do there) has the following in its source between elements

<BR>
<BR>
<BR>

how do I match this with a php regular expression?

+5  A: 

Something like this:

preg_match('/(<br>\s*){3}/i', $str, $matches);

This is a bit more lenient than your example - it does a case-insensitive match and matches any whitespace between the <br>s, not just newlines.

To match 3 or more instead of 3:

preg_match('/(<br>\s*){3,}/i', $str, $matches);
Greg
Isn't he trying to get the text between the <br> tags?
Yannick M.
I am not trying to replace all the <br> tags. Just the ones which appear 3 times in a row
Steven1350
@YannickI dont want the text between the tags, i just want to replace the 3 <br> tags with my own formatting
Steven1350
Greg, I think the pattern should use the `m` modifier instead of `s`.
Ionuț G. Stan
I see what you mean, sorry misinterpreted.
Yannick M.
@op: I see - edited to fix.@Ionut G. Stan: /m affects what ^ and $ do. /s affects . but since I'm not using that I removed it.
Greg
@Ionut — No, `s` is correct (though unnecessary, in this case). "Single-line mode" causes the dot operator to match newlines. "Multi-line mode" causes the `^` and `$` anchors to match after and before newlines, respectively.
Ben Blank
Yes, it is indeed unnecessary. And \s will match newlines. That is correct.
Ionuț G. Stan
+3  A: 

If you just want to replace the <BR> instances then you're better off doing a string replacement. It is a lot faster then regex.

$newstr = str_replace('<BR>', 'replacement...', $str);
bucabay
+1  A: 

My take on it

<?php

$html = <<<HTML
<BR>
<BR>
<BR>
<p>^^ Replace 3 consecutive BR tags with nothing</p>
<BR>
<BR>
<p>^^ those should stay, there's only 2 of them</p>
<BR>
  <BR>


      <BR>
<p>^^ But those should go, whitespace and newlines shouldn't matter
HTML;

echo preg_replace( "/(?:<br>\s*){3}/i", '', $html );
Peter Bailey