Home  >  Article  >  Backend Development  >  Replace line feed and carriage return in form input box in php_PHP tutorial

Replace line feed and carriage return in form input box in php_PHP tutorial

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

A summary of some methods for replacing line feeds and carriage returns in form input boxes in PHP. Friends in need can refer to this article.

The code is as follows Copy code
 代码如下 复制代码


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

?$str="this is a test n";
$patten = array("rn", "n", "r");

?//Replace rn first, then whether n exists, and finally replace r
代码如下 复制代码

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

$str=str_replace($order, "", $str);

?>

 代码如下 复制代码




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!';
?>







?//php There are three ways to solve it
The code is as follows Copy code
//1. Use str_replace to replace newlines $str = str_replace(array("rn", "r", "n"), "", $str); //2. Use regular replacement $str = preg_replace('//s*/', '', $str); //3. Use variables defined by php (recommended) $str = str_replace(PHP_EOL, '', $str); ?/* * Get the newline character of the user's operating system, 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';//The weight is bigger } Return $the_crlf; }
Test code
The code is as follows Copy code
Line break problem in PHP getting form area data ----------Separating line----------------------'."r "; if(!empty($content))echo str_replace("n",'nl',nl2br($content)); echo "n".'
----------Separating line----------------------'."n "; if(!empty($content))echo str_replace("r",'rl',str_replace("n",'nl',nl2br($content))); echo "r".'
----------Separating line----------------------
'."n"; echo 'hello'."n".'boys!'; echo 'hello'."r".'boys!'; ?>


1. The PHP function nl2br() inserts an HTML newline character before each new line (rn) in the string:
;
2. The newline under Windows is (rn);
3. In Notepad, r or n have the function of line break;

Note: When the front page is displayed, use nl2br to change the line break to

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632174.htmlTechArticleA summary of some methods for replacing line feeds and carriage returns in form input boxes in php. Friends in need can refer to this article . The code is as follows Copy the code ?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