I created a from which includes a textarea, this data will be saved to mysql, when I retrieve it, I want to only display the first two lines of this textarea data(ignore the rest), and only limit to text format(not html), how can I do that?
If I continue to type in textarea without hit return, it will show a long string after retrieving from mysql, I want to know how to control the display format.
You can easily parse or split text based on Returns ("\n"), but you are right, if the user does not hit Return, the text will wrap to another line in the textarea, but is actually not a seperate line. All you can do at that point, is break the string into "lines" based on character length. For example, you may figure that about 60 characters make up a line. You could then split the string into 60 character sections. You can even add logic to prevent splitting mid-word. But you still will not have a way to split the string exactly as it appeared in the textarea.
If you really must have this, you may have to use multiple text inputs instead of a textarea. (One text input per line.) This may or may not be an option in your case.
In the example you show, most likely, the user did in fact hit RETURN between those lines. If you are then displaying back in HTML, the two lines will show as one since in HTML, you have to specifically break a line with an HTML element such as <BR />.
What you can do is, just before displaying the string, use PHP's str_replace() function to replace "\n" with "<br />". I think this will give you what you want. http://us2.php.net/manual/en/function.str-replace.php
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.