- ? $str = "This is a test \ n";
- $patten = Array ("\ n", "\ n", "\ R");
- ?//replace \ r \ n First, then presence \ n
- $str =str_replace ($order, "", $str);
- ?>
Copy Code
PHP has three ways to solve
- 1. Use Str_replace to replace line breaks
- $str = Str_replace (Array ("\ r \ n", "\ r", "\ n"), "", $str);
2. Use regular replacement
- $str = preg_replace ('//s*/', ' ', $str);
3. Use PHP-defined variables (recommended)
- $str = Str_replace (Php_eol, ", $STR);
Copy Code
- /*
- * Get the user's operating system line break, \ 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 ';//The right to be significant
- }
- return $the _crlf;
- }
Copy CodeNote: When the foreground page is displayed, use NL2BR to change the line to
The second example illustrates: Find an interesting thing to do:
$text = "AAAA
- CCC ";
$text =str_replace (' \ n ', "", $text);
- $text =str_replace (' \ R ', "", $text);
- $text =str_replace (' \ r \ n ', ' ", $text);
Copy CodeNormally, the above code should be able to replace line breaks, but in fact it is not. Very depressed, tried many times, is not working. Finally, change to this:
- $text =str_replace ("\ n", "", $text);
- $text =str_replace ("\ R", "", $text);
- $text =str_replace ("\ r \ n", "", $text);
Copy CodeIncredibly on it, the original is double quotes, single quotation mark problem! Double quotation marks than single quote efficiency almost, because the double quotation marks in the process of PHP parsing, will also determine whether there are variables, single quotation marks will not have this judgment, so generally speaking, no variables involved in the case, I would use single quotation marks, did not expect this time to replace the newline character, with single quotation mark incredibly not Finally written:
- $order = Array ("\ n", "\ n", "\ R");
- $replace = ";
- $text =str_replace ($order, $replace, $text);
Copy CodeThis allows you to replace the line break. is not very convenient. >>> You may be interested in the article:php stripped string newline instance parsing php compressed HTML (clear line breaks, clear tabs, remove comment marks) PHP form to convert textarea line breaks PHP regular filter HTML tags , spaces, line breaks, and so on. PHP methods for removing line breaks summarize PHP compressed HTML page code (clear spaces, line breaks, tabs, comment markers, etc.) PHP generates Excel and controls line breaks in Excel cells |