Jump to content


Photo

Capturing newline characters a textbox


  • Please log in to reply
3 replies to this topic

#1 kiss-o-matic

kiss-o-matic
  • Members
  • PipPipPip
  • Advanced Member
  • 51 posts

Posted 10 August 2006 - 04:38 AM

I've got a text box that I'm using to store addresses.  The problem is, if I input my address like this:
"1234 Main St.
New York, NY,
11111"

Then it's stored in the database like this: "1234 Main St. New York, NY, 11111".

I tried this:
$addr = preg_replace("/\n/","<br>",$addr);
but it does not work. 

Thanks

#2 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 10 August 2006 - 04:45 AM

Try this.

$addr = ereg_replace("\r?\n", "<BR>\n", $addr);

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 August 2006 - 04:51 AM

No, it's stored fine. If you display it on with a browser, the browser doesn't use the newline character for a new line, it uses the <br /> tag. You need to pass your string through the function nl2br() when displaying it. This function precedes all newline characters in your string with the <br /> tag.

Ken

#4 kiss-o-matic

kiss-o-matic
  • Members
  • PipPipPip
  • Advanced Member
  • 51 posts

Posted 10 August 2006 - 05:00 AM

Very nice, Ken.
Thanks.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users