View Full Version : nl2br - Removing line drops

09-03-2006, 12:20 AM
Ok so im using the nl2br tag in my code but i find it makes my code look sloppy when in the code is shows the line drops with the <br /> tags in it. Is there a way of making my code a single straight line with the br's included??

09-03-2006, 07:22 AM
Post your code. That way, we'd be able to help you pretty it up better.

09-04-2006, 12:17 AM
$query = mysql_query("SELECT * FROM `shout` WHERE ORDER BY timestamp `desc`");
while($row = mysql_fetch_array($query)){
$id = $row["id"];
$name = $row["name"];
$body = $row["body"];

$body = nl2br($body);

echo $name.'<br/>'.$body.'<br/><br/>';


09-04-2006, 12:25 AM
That is because nl2br() only converts \n (linebreak) to <br />. To make the string "flat" you need to erase the \r (carriage return) too. This can be done by using str_replace()


$string = "Here´s a little text
with some
linebreaks in it";

echo nl2br($string);


Here´s a little text<br />
with some<br />
linebreaks in it

if you use

echo str_replace("\r\n", '<br />', $string);
the output becomes:

Here´s a little text<br />with some<br />linebreaks in it