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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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