p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: String Replacement


Message #1 by AnthonyBol@a... on Sun, 17 Feb 2002 19:01:43 EST
Hello,

I am trying to display text entered in a textarea field on a form in a web page and in an email.  I did a string replace on the text
data attempting to replace the line feed or return that is in the textarea with the <BR> tag.  Here is the code I used where
$special_instructions is the name of the textarea.  



if(!empty($special_instructions))   {

    $special_instructions = strip_tags(stripslashes($special_instructions));

    str_replace("\r\n","<br>",$special_instructions);

    $mail_body.="<br><br>$special_instructions";

}



I have used \r, \n, and \r\n as the string parameter to replace but nothing seems to work.  Am I using the wrong characters for
replacement?  Is it different in a textarea?  Thanks in advance.



Tony



Message #2 by "Lawrence" <lkrubner@g...> on Mon, 18 Feb 2002 03:56:07 -0500
You want function nl2br().



This on php.net:





nl2br

(PHP 3, PHP 4 >= 4.0.0)



nl2br --  Inserts HTML line breaks before all newlines in a string

Description

string nl2br ( string string)

Returns string with '<br />' inserted before all newlines.



Note: Starting with PHP 4.0.5, nl2br() is now XHTML compliant. All versions before 4.0.5 will return

string with '<br>' inserted before newlines instead of '<br />'.



See also htmlspecialchars(), htmlentities() and wordwrap().



http://www.php.net/manual/en/function.nl2br.php















----- Original Message -----

From: <AnthonyBol@a...>

To: "beginning php" <beginning_php@p...>

Sent: Sunday, February 17, 2002 7:01 PM

Subject: [beginning_php] String Replacement





> Hello,

> I am trying to display text entered in a textarea field on a form in a web page and in an email.

I did a string replace on the text data attempting to replace the line feed or return that is in the

textarea with the <BR> tag.  Here is the code I used where $special_instructions is the name of the

textarea.

>

> if(!empty($special_instructions))   {

>     $special_instructions = strip_tags(stripslashes($special_instructions));

>     str_replace("\r\n","<br>",$special_instructions);

>     $mail_body.="<br><br>$special_instructions";

> }

>

> I have used \r, \n, and \r\n as the string parameter to replace but nothing seems to work.  Am I

using the wrong characters for replacement?  Is it different in a textarea?  Thanks in advance.

>

> Tony

>

>











  Return to Index