PHP Gets the value of the textarea and handles the method of carriage return wrapping _php Tips

Source: Internet
Author: User
Tags explode html form

The example in this article describes how PHP gets the value of textarea and handles the method of carriage return line wrapping. Share to everyone for your reference. The implementation methods are as follows:

In general, in the HTML form textarea we press carriage return and newline are some ASCII or special character encoding, if not the conversion output text is not typesetting.

PHP Gets the textarea value is very simple textarea carriage return change behavior \ r \ n below See example
HTML code:

Copy Code code as follows:
<textarea name= "test" rows= "3″cols=" 20″id= "test" ></textarea>

PHP Code:

Copy Code code as follows:
$str =$_get[' test '];
echo $str. ' <br/> ';
$arr =explode ("\ n", $str);
Print_r ($arr);
echo Count ($arr). ' <br/> ';//return number
$str 1=nl2br ($STR);//Enter for line change
echo $str 1;

All code:

Copy Code code as follows:
<title></title>
<body>
<form action= "" >
<textarea name= "Test" ></textarea>
<input type= "Submit"/>
</form>
<?php
$str =$_get[' test '];
echo $str. ' <br/> ';
$arr =explode ("\ n", $str);
Print_r ($arr);
echo Count ($arr). ' <br/> ';//return number
$str 1=nl2br ($STR);//carriage return replaced by a newline default function
echo $str 1;
?>
</body>

Let's take a look at some of DZ forum examples. Not much to say, directly on the code:

Copy Code code as follows:
$names = Preg_split ('/\r\n/', $_post[' textarea '));
foreach ($names as $name) {
Todo something Eg:echo $name;
}

The value is very simple, then the assignment, in the TEXTAREA output line break is not so simple

Copy Code code as follows:
$vals = Get_from_mydb ();
$tmp = ';
foreach ($vals as $val) {
$tmp. = $val. ';
}

"and" "What does that mean?"
I believe we all see, in fact, textarea in the return to become a character in the "\ n", so the PHP processing textarea in the return is actually processing characters in the "\ n".

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.