Php obtains the textarea value and processes the carriage return and line feed.

Source: Internet
Author: User
This article mainly introduces how php can get the value of textarea and process the line break of carriage return, which involves conversion and processing of special characters. For more information, see

This article mainly introduces how php can get the value of textarea and process the line break of carriage return, which involves conversion and processing of special characters. For more information, see

This example describes how php can obtain the textarea value and process the carriage return and line feed. Share it with you for your reference. The specific implementation method is as follows:

In general, in an html form, in textarea, we press the carriage return and line feed to encode some ascii or special characters. If we do not convert the output text, it is not typographical.

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

The Code is as follows:


PHP code:

The 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); // press enter to change to line feed
Echo $ str1;


All code:

The Code is as follows:







<? Php
$ 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 the dz forum. Let's just move on to the Code:

The Code is as follows:

$ Names = preg_split ('/\ r \ n/', $ _ POST ['textarea ']);
Foreach ($ names as $ name ){
// Todo something eg: echo $ name;
}


The value is very simple, so the value assignment is not that easy to output line breaks in textarea.

The Code is as follows:

$ Vals = get_from_mydb ();
$ Tmp = '';
Foreach ($ vals as $ val ){
$ Tmp. = $ val .'';
}


What does "" and "" mean?
I believe everyone can see that, in fact, the carriage return in textarea becomes "\ n", so php processes the carriage return in textarea as "\ n" in the character ".

I hope this article will help you with PHP programming.

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.