Home  >  Article  >  Backend Development  >  php中表单输入框中换行回车替换_PHP教程

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

WBOY
WBOYOriginal
2016-07-13 10:56:231200browse

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;
}

测试代码

 

 代码如下 复制代码




PHP获取表单area数据中的换行问题


$content=empty($_POST['content'])?null:trim($_POST['content']);
if(!empty($content))echo str_replace("r",'rl',nl2br($content));
echo "r".'
----------分割线----------------------'."r";
if(!empty($content))echo str_replace("n",'nl',nl2br($content));
echo "n".'
----------分割线----------------------'."n";
if(!empty($content))echo str_replace("r",'rl',str_replace("n",'nl',nl2br($content)));
echo "r".'
----------分割线----------------------
'."n";
echo 'hello'."n".'boys!';
echo 'hello'."r".'boys!';
?>








1.PHP函数nl2br()是在字符串中的每个新行(rn)之前插入HTML换行符:
;
2.Windows下的换行是(rn);
3.在记事本中,r或n均有换行的功能;

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/632174.htmlTechArticlephp中表单输入框中换行回车替换的一些方法总结,有需要的朋友可参考一下本文章。 代码如下 复制代码 ?php ?$str=this is a test n; $patten = arra...
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