how to make text wrap and get line returns in a text entry box

They have: 140 posts

Joined: Nov 2003

Here's the situation:

I have a website where people fill out a form to get a profile that comes up with a number of pages.

One of the fields in the form is called Information/Overview where they can write some paragraphs about themslelves and tell people how wonderful they are.

What is happen is that when they do that and submit the entry... the information that they had input comes out like one long line with no breaks or returns or anything. I had this progrmamed for me about 4 years ago and never used it and now I am and find this out.

Is there a way to put in an html editor or make the data that was entered appear in the format it was input in? Hope this make sense.

I looked for the answer here but didn't find it.... tks.

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

If you look in the source code of the pages this script outputs is the formatting correct. i.e. are all the breaks in the HTML source code?

They have: 140 posts

Joined: Nov 2003

I think so. When I look at the source code it looks like how I typed it into the system with the breaks in lines in all, but it is running altogether as one long sentence when outputted to the screen.

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

When I look at the source code it looks like how I typed it into the system with the breaks in lines in all

That's what I was talking about, yes. The only whitespace a browser will take any notice of is a space (and it will ignore any other spaces after the first too), this is so that developers can format their code nicely (indent tags, enter comments etc.)

So when that system outputs the text exactly as it was entered, Web browsers will all ignore almost all of it. You need to convert those returns into <br /> or <p> ... </p>

Assuming you're using PHP, run the return from the database through this function:

function nl2p($text) {
  // Use \n for newline on all systems
  $text = preg_replace("/(\r\n¦\n¦\r)/", "\n", $text);

  // Only allow two newlines in a row.
  $text = preg_replace("/\n\n+/", "\n\n", $text);

  // Put <p>..</p> around paragraphs
  $text = preg_replace('/\n?(.+?)(\n\n¦\z)/s', "<p>$1</p>", $text);

  // convert newlines not preceded by </p> to a <br /> tag
  $text = preg_replace('¦(?<!</p> )\s*\n¦', "<br />", $text);

  return $text;
}

(above nabbed from: http://www.webmasterworld.com/forum88/4829.htm )

a Padded Cell our articles site!

They have: 140 posts

Joined: Nov 2003

Hi -

Thanks for the reply.

I'm not sure where to put that in the code........ i've tried a few spots but keep getting php errors. I'll keep trying but if you read this maybe you can make a recommendation.

Thanks again for your help.
Chris

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

It's a bit difficult without knowing how your application works. We'll need to see the bit of code that prints this to the page from the database, are you able to get that and paste it in here? Just put <code> tags around it.

Also, what error are you getting from PHP?

a Padded Cell our articles site!

He has: 629 posts

Joined: May 2007

Try adding the (non-standard) "wrap=soft" attribute to the textarea.

Let us know if this works, okay?

They have: 140 posts

Joined: Nov 2003

Hi-

I paid someone to do this because I needed it figured out... and they put in a "nl2br" tag and changed around a few brackets and it's working.

I tried to do it but couldn't.

Thanks for all the help and advice.

Chris

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Fair enough. If you needed the work done quickly without having to learn to code, that's the fastest way to do it. Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.