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 Quote Link to comment 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); ?> Quote Link to comment 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. Quote Link to comment 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; ?> Quote Link to comment 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; ?> Quote Link to comment 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>"; Quote Link to comment 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)); ?> Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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! Quote Link to comment 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>"; Quote Link to comment 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 Quote Link to comment 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.