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

Recommended Answers

All 8 Replies

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.

Thanks, Troy, I will give it a try.

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

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

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 ...

$entry_array = explode("\n", $_POST['textarea']);

if (empty($entry_array)){
   $entry_array = explode(" ", $_POST['textarea']);
}

foreach ($entry_array as $entry) {
   $insert_query = "INSERT `TABLE` VALUES(NULL, '$entry')";
   mysql_query("$insert_query");
}

Hello try this..

<td><?=substr($_POST['textarea'],0,20);?>.....</td>

You can print only some of the content...

Why don't just display the value from the database using nl2br() function?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.