Jump to content

Recommended Posts

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. 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/58898-questions-about-n-and-lines-of-string/
Share on other sites

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);
?>

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?

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.

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. 

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

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.