Jump to content

Wrapping in Textareas


WarKirby

Recommended Posts

Hi everyone. I'm trying to create a textarea in a form that will accept a list of strings, each exactly 9 characters long.

 

It seemed like the best way to do this was a textarea with 9 columns, and hard wrapping. So that it will force a linebreak every 9 chars, and I can use explode to parse it on the linebreak in PHP. It doesn't seem to be working as expected though.

 

firstly, I noticed that it wasn't wrapping at all. I was on firefox 3.0.somethinorother. I upgraded to the newest version and now that works, but I guess this means older firefox versions are bugged to not use wrapping properly.

 

Secondly, even now that it is working, it seems that despite the 9 columns, I can type 11 characters per line before it wraps.

 

Is this feature just unreliable? Ought I to parse the data some other way instead?

Link to comment
Share on other sites

Thank you for the reply. I was honestly more just trying to see if I could do it this way than anything. Since this form is entirely for my own covenience and will never be publicly available, I can live with having to be a little more careful. ATM, I'm not prepared to go and learn javascript on top of what I'm already doing, just for this. Perhaps in the future though.

 

Another quick question, just for clarification. If I hit enter while typing in a textarea, will a linebreak be submitted to the php script processing it at that point? And Is it going to be alright to use:

explode("<br />", $textFieldName);

in my php for the above case ?

Link to comment
Share on other sites

The enter key in text areas is submitted as a newline character, not an html br tag. The function nl2br() that vnpickle showed will convert the new line characters to html <br /> tags. You can then explode() on the <br /> tags. I find this conversion a painless way to explode textareas, as different systems use different newline characters, but nl2br seems to catch them all.

Link to comment
Share on other sites

So you use nl2br() to convert new lines in a textarea to <br /> tags and then use explode("<br />", $textarea_name) to remove them

Hence, next time you display it to a text area (eg. editing something) there will be no <br /> tags?

 

(sorry, i is n00b at PHP lol)

Link to comment
Share on other sites

Kind of. If I want an array with each line in it's own array key, I first use nl2br(), then I explode on the <br /> tags in order to put each line into its own key in the array. But if you are outputting to a textarea, you don't have to do any of this, as the linebreaks in the orignal text will act as line breaks inside the textarea. It's only when you are outputting the data as HTML that you need to use nl2br().

Link to comment
Share on other sites

Without knowing the software in question, and actually looking at it's storage procedure, it's impossible to say what they are doing. But the common way of doing things is to store the text in the database with the linebreaks as-is. Then when printing to the screen, nl2br() is used.

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.