Michan Posted September 21, 2008 Share Posted September 21, 2008 Hi, I seem to be stuck. I've tried replacing \n and \r, but it doesn't seem to have any effect on the code, which looks something like this: Line 1<br /> Line 2<br /> Line 3 How would I fix this with a function so it would display in HTML as the following? Line 1<br />Line 2<br />Line 3 Many thanks in advance, - Mi Link to comment https://forums.phpfreaks.com/topic/125151-solved-remove-html-whitespace/ Share on other sites More sharing options...
redarrow Posted September 21, 2008 Share Posted September 21, 2008 <?php echo nl2br("foo isn't\n bar"); ?> <?php echo nl2br("Welcome\r\nThis is my HTML document", false); ?> Link to comment https://forums.phpfreaks.com/topic/125151-solved-remove-html-whitespace/#findComment-646855 Share on other sites More sharing options...
Michan Posted September 21, 2008 Author Share Posted September 21, 2008 <?php echo nl2br("foo isn't\n bar"); ?> <?php echo nl2br("Welcome\r\nThis is my HTML document", false); ?> I already have line breaks in there, I'd just like to remove the additional whitespace as it's causing problems in Javascript. Link to comment https://forums.phpfreaks.com/topic/125151-solved-remove-html-whitespace/#findComment-646858 Share on other sites More sharing options...
redarrow Posted September 21, 2008 Share Posted September 21, 2008 <?php $name="1\n 2\n 3\n 4\n 5"; $n=nl2br($name); echo $n; ?> Link to comment https://forums.phpfreaks.com/topic/125151-solved-remove-html-whitespace/#findComment-646859 Share on other sites More sharing options...
redarrow Posted September 21, 2008 Share Posted September 21, 2008 <?php $name="1\n 2\n 3\n 4\n 5"; $n=nl2br($name); $x=str_replace(' ','',$n); echo $x; ?> Link to comment https://forums.phpfreaks.com/topic/125151-solved-remove-html-whitespace/#findComment-646862 Share on other sites More sharing options...
JasonLewis Posted September 21, 2008 Share Posted September 21, 2008 $str = <<<html Line 1<br /> Line 2<br /> Line 3 html; echo "<pre>".$str."</pre>"; echo "<pre>".str_replace(PHP_EOL,"",htmlentities($str))."</pre>"; Link to comment https://forums.phpfreaks.com/topic/125151-solved-remove-html-whitespace/#findComment-646864 Share on other sites More sharing options...
F1Fan Posted September 21, 2008 Share Posted September 21, 2008 You could try this: <?php echo str_replace('\n',str_replace('\r',$str)); ?> Link to comment https://forums.phpfreaks.com/topic/125151-solved-remove-html-whitespace/#findComment-646866 Share on other sites More sharing options...
JasonLewis Posted September 21, 2008 Share Posted September 21, 2008 Just use PHP_EOL, as it uses the appropriate End of Line depending on the OS. Also F1Fan, str_replace() takes 3 parameters. Link to comment https://forums.phpfreaks.com/topic/125151-solved-remove-html-whitespace/#findComment-646870 Share on other sites More sharing options...
F1Fan Posted September 21, 2008 Share Posted September 21, 2008 Yes, my bad. Forgot the replace (second param) string. Link to comment https://forums.phpfreaks.com/topic/125151-solved-remove-html-whitespace/#findComment-646873 Share on other sites More sharing options...
Michan Posted September 21, 2008 Author Share Posted September 21, 2008 Just use PHP_EOL, as it uses the appropriate End of Line depending on the OS. Also F1Fan, str_replace() takes 3 parameters. Thank you very much for this. It tidies it all onto a single line, but converts all of the <, >, ", ', &, etc into their HTML codes. Is there a way to remove the HTML returns without converting all of the HTML codes? Many thanks again! Link to comment https://forums.phpfreaks.com/topic/125151-solved-remove-html-whitespace/#findComment-646878 Share on other sites More sharing options...
JasonLewis Posted September 21, 2008 Share Posted September 21, 2008 Ha, yeh just remove the htmlentities(). I just had it there to show that it was putting it all into one line. $str = <<<html Line 1<br /> Line 2<br /> Line 3 html; echo "<pre>".$str."</pre>"; echo "<pre>".str_replace(PHP_EOL,"",$str)."</pre>"; Link to comment https://forums.phpfreaks.com/topic/125151-solved-remove-html-whitespace/#findComment-646879 Share on other sites More sharing options...
Michan Posted September 21, 2008 Author Share Posted September 21, 2008 Ha, yeh just remove the htmlentities(). I just had it there to show that it was putting it all into one line. Thank you very much for your time, this has helped a lot Link to comment https://forums.phpfreaks.com/topic/125151-solved-remove-html-whitespace/#findComment-646887 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.