Php gets the textarea value and processes the carriage return (line feed)

Source: Internet
Author: User
In the html form, in textarea, we press the carriage return and line feed to display some asc or special characters, but we cannot see them. if we do not convert the output text, there is no typographical layout, the following is a summary of some examples in this article. php get textare... in the html form, in textarea, we press the carriage return and line feed to display some asc or special characters, but we cannot see them. if we do not convert the output text, there is no typographical layout, the following is a summary of some examples in this article.

It is very simple for php to obtain the textarea value. The carriage return behavior \ r \ n is shown in the following example.

The HTML code is as follows:

 

The PHP code is as follows:

$ Str = $ _ GET ['test']; echo $ str .'
'; $ Arr = explode ("\ n", $ str); print_r ($ arr); echo count ($ arr ).'
'; // Number of carriage returns $ str1 = nl2br ($ str); // replace line breaks with echo $ str1;

All code:

      
 

$ Str = $ _ GET ['test'];

Echo $ str .'
';

$ Arr = explode ("\ n", $ str );

Print_r ($ arr );

Echo count ($ arr ).'
'; // Number of carriage returns

$ Str1 = nl2br ($ str); // press enter to change to the default function of line feed.

Echo $ str1;

Let's take a look at the example in a dz Forum. if you don't talk about it much, go to the code:

$ Names = preg_split ('/\ r \ n/', $ _ POST ['textarea ']); foreach ($ names as $ name) {// todo something eg: the echo $ name;} value is very simple, so the value assignment can be less simple. the code is as follows: $ vals = get_from_mydb (); $ tmp = ''; foreach ($ vals as $ val) {$ tmp. = $ val.'
';}

"" And" "What does it mean? I believe everyone can see it. In fact, the carriage return in textarea is converted into a character" \ n, therefore, php processes the carriage return in textarea and processes the "\ n" in the character ".


Article address:

Reprint at will ^ please include the address of this article!

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.