测试页面代码:
复制代码 代码如下:
<!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获取表单area数据中的换行问题</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/>----------分割线----------------------"."\r";
if(!empty($content))echo str_replace("\n","nl",nl2br($content));
echo "\n"."<br/>----------分割线----------------------"."\n";
if(!empty($content))echo str_replace("\r","rl",str_replace("\n","nl",nl2br($content)));
echo "\r"."<br/>----------分割线----------------------<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="提交" />
</form>
</body>
</html>
在浏览器中打开后,在表单中输入:
按提交后,浏览器中显示结果如下:
在记事本可可看到如下结果:
从以上结果可见:
1.PHP函数nl2br()是在字符串中的每个新行(\r\n)之前插入HTML换行符:<br/>;
2.Windows下的换行是(\r\n);
3.在记事本中,\r或\n均有换行的功能;