First type:
Copy Code code as follows:
<?php
? $str = "This is a test \ n";
$patten = Array ("\ r \ n", "\ n", "\ R");
?//First replace \ r \ n, then exist \ n, last replacement \ r
$str =str_replace ($order, "", $str);
?>
PHP has three ways to solve
1. Use Str_replace to replace line wrapping
$str = Str_replace (Array ("\ r \ n", "\ r", "\ n"), "", $str);
2, the use of regular replacement
$str = preg_replace ('//s*/', ', ', $str);
3. Use PHP to define a good variable (recommended)
$str = Str_replace (Php_eol, ', $str);
Copy Code code as follows:
/*
* Get line breaks for the user's operating system \ n
* @access Public
* @return String
*/
function Get_crlf ()
{
if (Stristr ($_server[' http_user_agent '], ' Win '))
{
$the _crlf = ' \ r \ n ';
}
ElseIf (Stristr ($_server[' http_user_agent '), ' Mac ')
{
$the _crlf = ' \ r '; For Old MAC OS
}
Else
{
$the _crlf = ' \ n ';
}
return $the _crlf;
}
Note: When the front page is displayed, use NL2BR to make the line break into <br>
The second example illustrates:
To find an interesting thing:
$text = "AAAA
CCC ";
$text =str_replace (' \ n ', "", $text);
$text =str_replace (' \ R ', ' ", $text);
$text =str_replace (' \ r \ n ', ' ", $text);
Normally, the code above should be able to replace the line feed.
But in fact it is not!
Very depressed, tried a lot of times, is not working.
This is the last change.
Copy Code code as follows:
$text =str_replace ("\ n", "", $text);
$text =str_replace ("R", "", $text);
$text =str_replace ("\ r \ n", "", $text);
Incredibly all OK ~ ~, the original is double quotes, single quotes problem!!
Double quotes are almost as efficient as single quotes, because the double quotes in PHP parsing process, but also judge whether there will be variables, single quotes will not have this judgment, so generally speaking, not involved in the case of variables, I will use single quotes, did not expect this replacement line character, with single quotes not even
Finally write a sentence
Copy Code code as follows:
$order = Array ("\ r \ n", "\ n", "\ R");
$replace = ';
$text =str_replace ($order, $replace, $text);
This replaces the newline character!