Summary of several methods for replacing newline characters in PHP _php tutorial

Source: Internet
Author: User
The first type:
Copy CodeThe code is as follows:
? $str = "This is a test \ n";
$patten = Array ("\ n", "\ n", "\ R");
?//replace \ r \ n First, then presence \ n
$str =str_replace ($order, "", $str);
?>

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 CodeThe code is as follows:
/*
* 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;
}

Note: 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);

Normally, the above code should be able to replace the line break.

But in fact it is not!

Very depressed, tried many times, is not working.

And finally changed it.
Copy CodeThe code is 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 quotation marks, 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 write a sentence
Copy CodeThe code is as follows:
$order = Array ("\ n", "\ n", "\ R");
$replace = ";
$text =str_replace ($order, $replace, $text);

This replaces the line break!

http://www.bkjia.com/PHPjc/326211.html www.bkjia.com true http://www.bkjia.com/PHPjc/326211.html techarticle The First: Copy the code as follows: php? $str = "This is a test \ n"; $patten = Array ("\ r \ n", "\ n", "\ R"); Str_replac ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.