Several ways to replace newline characters in PHP

Source: Internet
Author: User
A line break is a computer language expression that jumps to the next new line. In different languages, the code is different. such as:<br>, "\ n", "Endl" and so on. This article mainly introduces several methods of replacing newline characters in PHP

The first type:

<?php? $str = "This is a test \ n"; $patten = Array ("\ r \ n", "\ R"), or \//replace \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ $str =str_replace ($order, "", $str);?>
PHP has three ways to resolve//1, use Str_replace to replace line break $str = Str_replace ("\ r \ n", "\ r", "\ n"), "", $str); 2. Use regular replacement $str = Preg_replace ('//s*/', ' ', $str); 3, the use of PHP defined variables (recommended) $STR = Str_replace (Php_eol, ", $STR);
/* 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 the old Mac OS } else {$the _crlf = ' \ n ';//A significant point} return $the _crlf; }

Note: When the foreground page is displayed, use NL2BR to change the line into <br>

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.

The 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

The code is as follows:

$order = Array ("\ n", "\ n", "\ R"); $replace = "; $text =str_replace ($order, $replace, $text);

This replaces the line break!

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.