Jump to content

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
https://forums.phpfreaks.com/topic/156534-remove-repeating-s/
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
https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824230
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
https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824236
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
https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824791
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
https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-825048
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.