PHP头条
热点:

php中表单输入框中换行回车替换


php中表单输入框中换行回车替换的一些方法总结,有需要的朋友可参考一下本文章。
 代码如下 复制代码


<?php

?$str="this is a test n";
$patten  = array("rn", "n", "r");  
?//先替换掉rn,然后是否存在n,最后替换r 
$str=str_replace($order, "", $str);
?>

 

?//php 有三种方法来解决  
 

 代码如下 复制代码

//1、使用str_replace 来替换换行 
$str = str_replace(array("rn", "r", "n"), "", $str); 
 
//2、使用正则替换 
$str = preg_replace('//s*/', '', $str); 
 
//3、使用php定义好的变量 (建议使用) 
$str = str_replace(PHP_EOL, '', $str);


?/*
* 获得用户操作系统的换行符,n
* @access public
* @return string
*/
function get_crlf()
{
    if (stristr($_SERVER['HTTP_USER_AGENT'], 'Win'))
    {
        $the_crlf = 'rn';
    }
    elseif (stristr($_SERVER['HTTP_USER_AGENT'], 'Mac'))
    {
        $the_crlf = 'r'; // for old MAC OS
    }
    else
    {
        $the_crlf = 'n';//权重大一点
    }
    return $the_crlf;
}

测试代码

 

 代码如下 复制代码
<!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()是在字符串中的每个新行(rn)之前插入HTML换行符:<br/>;
2.Windows下的换行是(rn);
3.在记事本中,r或n均有换行的功能;

注意:在前台页面显示的时候,用nl2br使换行变成<br>

www.phpzy.comtrue/php/26746.htmlTechArticlephp中表单输入框中换行回车替换 php中表单输入框中换行回车替换的一些方法总结,有需要的朋友可参考一下本文章。 代码如下 复制代码 ?php ?$str=this is a test n; $patten = array(rn, n, r); ?//先...

相关文章

PHP之友评论

今天推荐