This article mainly introduces how php obtains the textarea value and processes the carriage return line feed, which involves the conversion and processing of special characters, for more information about how to use php to obtain the textarea value and handle carriage return and line feed, see the example below. 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.