Home >php教程 >php手册 >php逐行读取textarea及向textarea输出换行符

php逐行读取textarea及向textarea输出换行符

WBOY
WBOYOriginal
2016-05-25 16:50:062920browse

最近在研究discuz,发现后台很多设置项,都用textarea填写,然后每行一项,感觉很有意思,嘿嘿,主要是很简单,免得在设置的时候为控件的问题浪费太多时间。

不多说了,上代码:

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

取值很简单,那么赋值呢,在textarea中输出换行符可没那么简单

$vals = get_from_mydb();
$tmp = '';
foreach($vals as $val){
    $tmp .= $val.'
';
}

" "和" "是什么意思,因为时间为题我就不说了,自己去百度 GG一下吧!

<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>PHP获取表单area数据中的换行问题</title> 
</head> 
<body> 
<?php 
$content=empty($_POST[&#39;content&#39;])?null:trim($_POST[&#39;content&#39;]); 
if(!empty($content))echo str_replace("r",&#39;rl&#39;,nl2br($content)); 
echo "r".&#39;<br/>----------分割线----------------------&#39;."r"; 
if(!empty($content))echo str_replace("n",&#39;nl&#39;,nl2br($content)); 
echo "n".&#39;<br/>----------分割线----------------------&#39;."n"; 
if(!empty($content))echo str_replace("r",&#39;rl&#39;,str_replace("n",&#39;nl&#39;,nl2br($content))); 
echo "r".&#39;<br/>----------分割线----------------------<br/>&#39;."n"; 
echo &#39;hello&#39;."n".&#39;boys!&#39;; 
echo &#39;hello&#39;."r".&#39;boys!&#39;; 
?> 
<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()是在字符串中的每个新行(rn)之前插入HTML换行符:
;
2.Windows下的换行是(rn);
3.在记事本中,r或n均有换行的功能;

文章网址:

随意转载^^但请附上教程地址。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn