Jump to content

Adding line breaks


Eiolon

Recommended Posts

[code]<?php
$text = (get_magic_quotes_gpc() ? stripslashes($_POST["text"]) : $_POST["text"]);
$text = mysql_real_escape_string(nl2br($text));
?>[/code]

stripslashes and mysql_real_escape_string are there to prevent malicious input. nl2br is the function you needed. It converts new-line characters (\n) to HTML line-breaks (< br />).

[b][EDIT:][/b]

utexas_pjm posted before me...but...same answer.
Link to comment
https://forums.phpfreaks.com/topic/30397-adding-line-breaks/#findComment-139877
Share on other sites

Michan that's precisely what PHP's built-in nl2br function does. But good thinking!

Just FYI though HTML now requires (for the standard though most browsers will accept the old method) all tags that don't have a closing tag, i.e., < br />, < hr />, < img />, etc., to end with "/>" instead of just ">". That signifies the closing of these single-tag tags. ;)
Link to comment
https://forums.phpfreaks.com/topic/30397-adding-line-breaks/#findComment-139885
Share on other sites

If you really want to use [b]str_replace[/b] then I suggest you replace also [b]\r[/b] and [b]\r\n[/b].
[code=PHP]$string = str_replace (array ("\r\n", "\n", "\r"), "<br />", $string);[/code]

[b]nl2br[/b] will take care of all three ways.

[b]\n[/b] = *Nix
[b]\r[/b] = Windows
[b]\r\n[/b] = Mac
Link to comment
https://forums.phpfreaks.com/topic/30397-adding-line-breaks/#findComment-139930
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.