1

As the title says I'm trying to remove and replace three or more <br> from get_the_content(); in my single.php.

I'm currently using this but it doesn't seem to have any affect on the amount of <br> in the document.

$content = get_the_content();
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);
$content = preg_replace('/(<br[\s]?[\/]?>[\s]*){3,}/', '<br /><br />', $content);
$content = strip_tags($content, '<br> <p> <a>');
echo $content; 

This gives me the output:

<br />
<br />
<br />
<a href="http://1.bp.blogspot.com/-rYRPguEjEl8/UrSWUC_jEOI/AAAAAAAANm4/qVle4zdVfsQ/s1600/Alexa+NYC.jpg" imageanchor="1"></a>

<p>
<br />
<br />You don&#8217;t always have to stick to the same colour scheme 
<br />
<br />
<br />
<br />

I can't seem to get the regex to work correctly.

UzumakiDev
  • 1,286
  • 2
  • 17
  • 39
  • 1
    Don't use regex for HTML! http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Connor Peet Jan 01 '14 at 15:14
  • 4
    @ConnorPeet People on SO LOVE to say "don't use regex for HTML!"... but then they don't give a solution because using DOM or simpleXML to solve the above issue is a clusterF and they end up using regex to solve it. Just like the accepted answer to this one: http://stackoverflow.com/questions/3627489/php-parse-html-code – Digital Chris Jan 01 '14 at 15:46
  • 1
    @DigitalChris I'm using DOM methods for other stuff but as this is just duplicate tags I was under the impression using regex would be fine. I've seen that regex post so many times :) – UzumakiDev Jan 01 '14 at 15:50
  • As someone very comfortable in Js and the "real" DOM API, I like DOMDocument. You do have a fair point, though. – Connor Peet Jan 01 '14 at 15:53

1 Answers1

0

Try this code:

<?PHP
$input ="<br/><br/><br/><br/>Hello";
highlight_string($input);
$output = preg_replace('/(<br\s*\/?>\s*){3,}/', '<br/><br/>', $input);
highlight_string($output);
?>
Zword
  • 6,605
  • 3
  • 27
  • 52