PHP gets the newline problem in the textarea data of the form

  • 2020-03-31 21:17:01
  • OfStack

Test page code:
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>PHP Get the form area Line breaks in data </title> 
</head> 
<body> 
<?php 
$content=empty($_POST['content'])?null:trim($_POST['content']); 
if(!empty($content))echo str_replace("r",'rl',nl2br($content)); 
echo "r".'<br/>---------- The divider ----------------------'."r"; 
if(!empty($content))echo str_replace("n",'nl',nl2br($content)); 
echo "n".'<br/>---------- The divider ----------------------'."n"; 
if(!empty($content))echo str_replace("r",'rl',str_replace("n",'nl',nl2br($content))); 
echo "r".'<br/>---------- The divider ----------------------<br/>'."n"; 
echo 'hello'."n".'boys!'; 
echo 'hello'."r".'boys!'; 
?> 
<form action="textareanl.php" method="post" enctype="multipart/form-data"> 
<textarea name="content" cols="20" rows="6"></textarea> 
<br /> 
<input type="submit" value=" submit " /> 
</form> 
</body> 
</html> 

After opening in the browser, enter in the form:

< img border = 0 SRC = "http://files.jb51.net/upload/201009/20100910212023405.jpg" >

After clicking submit, the browser displays the results as follows:

< img border = 0 SRC = "http://files.jb51.net/upload/201009/20100910212023894.jpg" >

 

In notepad cocoa see the following results:

< img border = 0 SRC = "http://files.jb51.net/upload/201009/20100910212023640.jpg" >

As can be seen from the above results:

1. The PHP function nl2br() inserts the HTML newline character before each new line in the string (\r\n) :< Br / >;
2. The newline in Windows is (\r\n);
3. In notepad,\r or \n have the function of line breaking;


Related articles: