Home  >  Article  >  Backend Development  >  PHP method to get the value of textarea and process carriage return and line feed, textarea carriage return and line feed_PHP tutorial

PHP method to get the value of textarea and process carriage return and line feed, textarea carriage return and line feed_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:16:351077browse

php method to get the value of textarea and process carriage return and line feed, textarea carriage return and line feed

The example in this article describes how PHP obtains the value of textarea and handles carriage returns and line feeds. Share it with everyone for your reference. The specific implementation method is as follows:

Generally speaking, in the textarea in the HTML form, when we press enter and line feed, they are encoded by some ASCII or special characters. If the conversion is not performed, the output text will not be typeset.

It is very simple for PHP to get the value of textarea. Textarea carriage return behavior rn Let’s see an example
HTML code:

Copy code The code is as follows:

PHP code:

Copy 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); // Carriage return is replaced by line feed
echo $str1;

All codes:

Copy 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);//Default function for carriage return to line break
echo $str1;
?>


Let’s look at an example from the dz forum. Not much more to say, just go to the code:

Copy code The code is as follows:
$names = preg_split('/rn/',$_POST['textarea']);
foreach($names as $name){
// todo something eg: echo $name;
}

Getting the value is very simple, but as for the assignment, outputting the newline character in the textarea is not that simple

Copy code The code is as follows:
$vals = get_from_mydb();
$tmp = '';
foreach($vals as $val){
$tmp .= $val.' ';
}

" "and" "What does it mean
I believe you have all seen it. In fact, the carriage return in the textarea becomes the character "n", so when php handles the carriage return in the textarea, it actually processes the "n" in the character.

I hope this article will be helpful to everyone’s PHP programming design.

In PHP or HTML, after the content in