citricsquid Posted May 2, 2009 Share Posted May 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/ Share on other sites More sharing options...
ignace Posted May 2, 2009 Share Posted May 2, 2009 Did you try: <?php // content negotiation $supportXhtml = stristr($_SERVER['HTTP_ACCEPT'], 'application/xhtml+xml') !== false?true:false; $br = nl2br("\n", $supportXhtml); $brs = str_replace($br.$br, $br, $brs); ?> Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824226 Share on other sites More sharing options...
jackpf Posted May 2, 2009 Share Posted May 2, 2009 $str = preg_replace('/\<br \/\>{2,}/', '<br />', $str); Might work. If not, something like that. Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824228 Share on other sites More sharing options...
Daniel0 Posted May 2, 2009 Share Posted May 2, 2009 $str = preg_replace('#(<br ?/?>){2,}#i', '', $str); Should work. Jack, the quantifier in your pattern is the the closing >. You need to group it in parenthesis to have it work on multiple characters. Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824229 Share on other sites More sharing options...
kenrbnsn Posted May 2, 2009 Share Posted May 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824230 Share on other sites More sharing options...
citricsquid Posted May 2, 2009 Author Share Posted May 2, 2009 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 :| Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824236 Share on other sites More sharing options...
citricsquid Posted May 2, 2009 Author Share Posted May 2, 2009 Still doesn't work. How strange. The HTML output looks like: <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> but using \S and \s doesn't help. hmmm. Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824241 Share on other sites More sharing options...
Daniel0 Posted May 2, 2009 Share Posted May 2, 2009 Try this then: $str = preg_replace('#(<br ?/?>){2,}\s*#i', '', $str); \s means any whitespace character. Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824242 Share on other sites More sharing options...
citricsquid Posted May 2, 2009 Author Share Posted May 2, 2009 Try this then: $str = preg_replace('#(<br ?/?>){2,}\s*#i', '', $str); \s means any whitespace character. Still not working. How strange :| Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824244 Share on other sites More sharing options...
jackpf Posted May 2, 2009 Share Posted May 2, 2009 Oh yeah, didn't notice that Daniel0. Cheers. And wouldn't you have to use \n rather than \s? Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824265 Share on other sites More sharing options...
cringe Posted May 2, 2009 Share Posted May 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824332 Share on other sites More sharing options...
jackpf Posted May 2, 2009 Share Posted May 2, 2009 No, he wants one line break from any number of line breaks. The {2,} matches 2 or more. Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824456 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2009 Share Posted May 2, 2009 citricsquid - mind posting the code you have thus far? Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824457 Share on other sites More sharing options...
cringe Posted May 2, 2009 Share Posted May 2, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824464 Share on other sites More sharing options...
jackpf Posted May 2, 2009 Share Posted May 2, 2009 Yeah, that's what I originally put. I guess someone changed it while editing it. Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824527 Share on other sites More sharing options...
Daniel0 Posted May 3, 2009 Share Posted May 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824791 Share on other sites More sharing options...
jackpf Posted May 3, 2009 Share Posted May 3, 2009 Nice work Oh, I thought \s didn't include \n. Meh. Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824793 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 "<br>" is xHTML-compliant. Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-824794 Share on other sites More sharing options...
cringe Posted May 3, 2009 Share Posted May 3, 2009 "<br>" is xHTML-compliant. It is?? I thought the XHTML syntax was "<br />" . Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-825024 Share on other sites More sharing options...
gevans Posted May 3, 2009 Share Posted May 3, 2009 "<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 /> Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-825031 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 "<br>" is xHTML-compliant. It is?? I thought the XHTML syntax was "<br />" . I meant to put a question mark. Sorry. It was intended to point out it's not. Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-825036 Share on other sites More sharing options...
nrg_alpha Posted May 3, 2009 Share Posted May 3, 2009 Nice work 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. Quote Link to comment https://forums.phpfreaks.com/topic/156534-remove-repeating-s/#findComment-825048 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.