First type:
Copy code The code is as follows:
?$str= "this is a test n";
$patten = array("rn", "n", "r");
?//Replace rn first, then whether n exists, and finally replace r
$str=str_replace($order, "", $str);
?>
//php There are three ways to solve
//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);
Copy code The code is as follows:
/*
* Get user Newline character of the 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;
}
Note: When the front page is displayed, use nl2br to change the line break to
The second example:
I found an interesting thing:
$text="aaaa
ccc";
$text=str_replace(' n',"",$text);
$text=str_replace('r',"",$text);
$text=str_replace('rn',"",$text);
Normally speaking, the above code should be able to replace the newline character
But in fact it is not!
I’m very depressed. I tried many times but it just doesn’t work.
Finally changed it to this
Copy the code The code is as follows:
$text=str_replace("n" ,"",$text);
$text=str_replace("r","",$text);
$text=str_replace("rn","",$text);
Everything is OK~~, it turned out to be a problem of double quotes and single quotes! !
Double quotation marks are less efficient than single quotation marks, because when double quotation marks are parsed by PHP, they will also judge whether there are variables in them, but single quotation marks will not make this judgment, so generally speaking, there is no Whenever variables are involved, I always use single quotes. I didn’t expect that replacing the newline character this time would not work with single quotes...
Finally, I wrote it in one sentence
Copy code The code is as follows:
$order = array("rn", "n", "r");
$replace = '';
$text=str_replace($order, $replace, $text);
This will replace the newline character!
http://www.bkjia.com/PHPjc/326211.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326211.htmlTechArticleFirst type: Copy the code as follows: ?php ?$str="this is a test n"; $ patten = array("rn", "n", "r"); ?//Replace rn first, then whether n exists, and finally replace r $str=str_replac...