WarKirby Posted December 30, 2009 Share Posted December 30, 2009 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? Quote Link to comment Share on other sites More sharing options...
vinpkl Posted December 30, 2009 Share Posted December 30, 2009 if you need linebreak after certain number of character then you need to use javascript while displaying your textarea content. vineet Quote Link to comment Share on other sites More sharing options...
WarKirby Posted December 30, 2009 Author Share Posted December 30, 2009 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 ? Quote Link to comment Share on other sites More sharing options...
vinpkl Posted December 30, 2009 Share Posted December 30, 2009 if you want to get a line break on hitting enter key, then you can use <?php echo nl2br("$textarea"); ?> http://php.net/manual/en/function.nl2br.php vineet Quote Link to comment Share on other sites More sharing options...
JustLikeIcarus Posted December 31, 2009 Share Posted December 31, 2009 You will have to like vinpkl stated or alter the parsing that php does and parse based on '\n' which is what hitting the enter key in a textarea inputs. One of the nicest ways to do what you want is by using a tokenizing plugin but that is javascript. Quote Link to comment Share on other sites More sharing options...
haku Posted January 4, 2010 Share Posted January 4, 2010 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. Quote Link to comment Share on other sites More sharing options...
Tazerenix Posted January 5, 2010 Share Posted January 5, 2010 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) Quote Link to comment Share on other sites More sharing options...
haku Posted January 5, 2010 Share Posted January 5, 2010 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(). Quote Link to comment Share on other sites More sharing options...
Tazerenix Posted January 5, 2010 Share Posted January 5, 2010 I say this in the context of like a blogging post. So when you add it to the database/post it o a page on your site it has the <br /> tags on but if you went to an admin section to edit the post the <br /> tags could be removed? Quote Link to comment Share on other sites More sharing options...
haku Posted January 6, 2010 Share Posted January 6, 2010 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. Quote Link to comment Share on other sites More sharing options...
Tazerenix Posted January 6, 2010 Share Posted January 6, 2010 ahh alright, thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.