zfred09 Posted July 8, 2007 Share Posted July 8, 2007 How does php determine when to put a \n into a string when it is submitted by a form? The reason I ask is because I'm trying to determine a way to count the number of lines in a string submitted via a text field. Currently I'm exploding the string at \n to get the number of lines, but when I echo the text the number I get from exploding matches neither the shown \n's nor the number of lines. Quote Link to comment https://forums.phpfreaks.com/topic/58898-questions-about-n-and-lines-of-string/ Share on other sites More sharing options...
AndyB Posted July 8, 2007 Share Posted July 8, 2007 php replaces each 'enter' with a newline (\n) character in a normal form. this\nis a\ntest explode will give you three array elements - three lines even though there are only 2 \n <?php $str="this\nis a\ntest"; $lines = explode("\n", $str); echo count($lines); ?> Quote Link to comment https://forums.phpfreaks.com/topic/58898-questions-about-n-and-lines-of-string/#findComment-292236 Share on other sites More sharing options...
zfred09 Posted July 8, 2007 Author Share Posted July 8, 2007 Ok, so say I have a text area, like this box I am typing in, how does it determine when to insert a \n because I never press enter at the end of the line and I could always resize the text area. Quote Link to comment https://forums.phpfreaks.com/topic/58898-questions-about-n-and-lines-of-string/#findComment-292251 Share on other sites More sharing options...
AndyB Posted July 8, 2007 Share Posted July 8, 2007 It doesn't insert a newline in the text. What you're seeing is the perfectly normal behaviour of browser elements. Quote Link to comment https://forums.phpfreaks.com/topic/58898-questions-about-n-and-lines-of-string/#findComment-292255 Share on other sites More sharing options...
zfred09 Posted July 8, 2007 Author Share Posted July 8, 2007 How does php determine where the \n's go? Quote Link to comment https://forums.phpfreaks.com/topic/58898-questions-about-n-and-lines-of-string/#findComment-292259 Share on other sites More sharing options...
AndyB Posted July 8, 2007 Share Posted July 8, 2007 php has NOTHING to do with what's happening in your browser while you resize things. php puts an \n in a string replacing every newline (like 'ENTER') when the string is submitted to the server. It doesn't 'determine' where to put them, ot just blindly replaces them wherever they are. Do you have a working example of some different behaviour? Quote Link to comment https://forums.phpfreaks.com/topic/58898-questions-about-n-and-lines-of-string/#findComment-292261 Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 a text field as you can test in the inputting on this forumn does not use any newlines/breaklines/enters instead it uses a method of word wrap where if the text reaches the end of the line (num chars > cols) it will wrap the next word to the next line. I don't see why you would need to know the number of lines in a text field that seems to be irrelevant to anything. Explain its uses and maybe we can suggest a better method for this end you seek. Quote Link to comment https://forums.phpfreaks.com/topic/58898-questions-about-n-and-lines-of-string/#findComment-292266 Share on other sites More sharing options...
zfred09 Posted July 8, 2007 Author Share Posted July 8, 2007 No, I understand what's happening in my browser. How does php determine what is a newline? Certain number of characters? I am wanting to count the number of lines, for a pagination technique that I am trying, instead of entering each separate paragraph into a db, I am inserting a key into the string before it is stored in the db so when it comes out user end I merely explode the string at that key. I have it working based on characters, but the problem with that is if a user inserted many paragraphs, it still takes up the physical space, but not the characters. So I am looking for a way to count the number of lines instead. Quote Link to comment https://forums.phpfreaks.com/topic/58898-questions-about-n-and-lines-of-string/#findComment-292270 Share on other sites More sharing options...
AndyB Posted July 8, 2007 Share Posted July 8, 2007 Every time php encounters a newline in a string it replaces it with the \n character. If you type 20,000 words in a single paragraph there are no new lines, regardless of how wide your screen is or how wide the text box used to enter the 20,000 words. What you really want to do is take the input from your form, put it in the database, and when you retrieve it and want to display it, use the php function nl2br() on it - new lines to br tags. http://ca.php.net/manual/en/function.nl2br.php Quote Link to comment https://forums.phpfreaks.com/topic/58898-questions-about-n-and-lines-of-string/#findComment-292273 Share on other sites More sharing options...
zfred09 Posted July 8, 2007 Author Share Posted July 8, 2007 Alright I'll check that out tomorrow and reply back how it goes. For now thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/58898-questions-about-n-and-lines-of-string/#findComment-292276 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.