Several ways to replace line breaks in PHP summary _php tips

Source: Internet
Author: User

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!

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.