Jump to content

Remove repeating <br/>'s?


citricsquid

Recommended Posts

Hi,

I have a string like so:

 

Hello<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>you!

 

And I only want it to display a single <br/> at most, so if there's

 

<br/><br/>

 

it replaces it with <br/>, or if there's

 

<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>

 

it replaces it with <br/>.

 

I tried str_replace and that didn't work :(

Link to comment
Share on other sites

You could use a while statement:

<?php
$str = 'Hello<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>you!';
while (strpos($str,'<br/><br/>') !== false)
   $str = str_replace('<br/><br/>','<br/>',$str);
echo $str;
?>

 

Ken

Link to comment
Share on other sites

Thanks for the help so far. I've been trying variations of those for ages now, including using a while loop and regex. I just realised that it's not formatted like:

 

<br/><br/><br/>

 

it's formatted like:

 

<br/>
<br/>
<br/>

 

So I'm going to see if adding in linebreaks sorts it out, that seems to be the problem :|

Link to comment
Share on other sites

Try this then:

 

$str = preg_replace('#(<br ?/?>){2,}\s*#i', '', $str);

\s means any whitespace character.

 

Doesn't this replace 2 or more <br/> with nothing? The requirement is to replace 2 or more with 1. So the '' needs to be '<br />' I would think.

 

Link to comment
Share on other sites

No, he wants one line break from any number of line breaks.

The {2,} matches 2 or more.

 

yes, so Hello<br /><br />World becomes HelloWorld.  It should be Hello<br />World. Therefore, the preg_replace should have a replacement value of '<br />', not ''. Correct?

Link to comment
Share on other sites

How about this:

 

$str = <<<EOF
foo bar <br /><br/><br>

<BR>hello<br>test
EOF;

$str = preg_replace('#(<br ?/?>\s*)+#i', '<br>', $str);

echo $str;

 

That will output:

foo bar <br>hello<br>test

 

And wouldn't you have to use \n rather than \s?

No, \s means any whitespace character (line break included). If you just used \n then it wouldn't work if there were e.g. spaces or tabs in between.

Link to comment
Share on other sites

"<br>" is xHTML-compliant.

 

It is?? I thought the XHTML syntax was "<br />" .

 

 

You're correct. Every tag in XHTML requires a closing tag. So single open/close tags such as br or hr need to be closed themselves;

 

<br />

 

or

 

<hr />

Link to comment
Share on other sites

Nice work :P

 

Oh, I thought \s didn't include \n. Meh.

 

Generally, \s could be considered the equivalent to [ \t\v\f\r\n] (for ASCII systems) Granted, PCRE doesn't support vertical tabs for instance. But yeah, \n is definitely considered part of \s. You can read about \s in more depth at pcre.org. Simply use ctrl + f to launch your browser's 'find' functionality, and enter \s and you'll quickly be brought to that section.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.