944,031 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 23491
  • PHP RSS
Jun 16th, 2005
0

how to format the form textarea?

Expand Post »
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.

thanks for any help.

michael
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
michael123 is offline Offline
93 posts
since Jun 2005
Jun 16th, 2005
0

Re: how to format the form textarea?

To remove code tags, check out PHP's strip_tags function.
http://us3.php.net/manual/en/function.strip-tags.php

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.
Reputation Points: 36
Solved Threads: 6
Posting Whiz
Troy is offline Offline
354 posts
since Jun 2005
Jun 16th, 2005
0

Re: how to format the form textarea?

Thanks, Troy, I will give it a try.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
michael123 is offline Offline
93 posts
since Jun 2005
Jun 17th, 2005
0

Re: how to format the form textarea?

I have a <textarea></textarea> in this textarea, people can type anything they want, for example:
in <textarea> people will write like;

123456789
abcdefghigk

and save to the database. but when I get it from database and write to the html it become

123456789abcdefghigk


I cannot simply count the character and wrap it, is there a way to use function(if available) to do it?

Thanks
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
michael123 is offline Offline
93 posts
since Jun 2005
Jun 17th, 2005
0

Re: how to format the form textarea?

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
Reputation Points: 36
Solved Threads: 6
Posting Whiz
Troy is offline Offline
354 posts
since Jun 2005
Jun 17th, 2005
0

Re: how to format the form textarea?

PoA
Reputation Points: 19
Solved Threads: 9
Posting Whiz in Training
PoA is offline Offline
234 posts
since Jul 2004
Jul 9th, 2008
0

Re: how to format the form textarea?

I think you would be better off using explode instead of preg_replace..

To make each line (eg; they did hit enter .. ) but just in case, try exploding on spaces if explode on newline failed ...

php Syntax (Toggle Plain Text)
  1. $entry_array = explode("\n", $_POST['textarea']);
  2.  
  3. if (empty($entry_array)){
  4. $entry_array = explode(" ", $_POST['textarea']);
  5. }
  6.  
  7. foreach ($entry_array as $entry) {
  8. $insert_query = "INSERT `TABLE` VALUES(NULL, '$entry')";
  9. mysql_query("$insert_query");
  10. }
Last edited by peter_budo; Jul 10th, 2008 at 10:04 am. Reason: Correcting code tags, not [code language=php] but [code=php]
mwa
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mwa is offline Offline
7 posts
since Sep 2004
Jul 10th, 2008
0

Re: how to format the form textarea?

Hello try this..

php Syntax (Toggle Plain Text)
  1. <td><?=substr($_POST['textarea'],0,20);?>.....</td>

You can print only some of the content...
Last edited by Shanti C; Jul 10th, 2008 at 1:14 am.
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Sep 15th, 2011
0
Re: how to format the form textarea?
Why don't just display the value from the database using nl2br() function?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cicovy is offline Offline
2 posts
since Sep 2010

This thread is more than three months old

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.
Message:
Previous Thread in PHP Forum Timeline: Email array values from checkbox...?
Next Thread in PHP Forum Timeline: Process multiple form inputs PHP?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC