PHP method Summary for removing line breaks (use of php_eol variables)

Source: Internet
Author: User

Originally in the UNIX world to replace the line with/n instead, but windows in order to reflect his differences, the use of/r/n, more interesting is in the Mac with/R. So the UNIX series with the/n,windows series with the/r/n,mac with/R, so that you write the program on different platforms run with a lot of trouble

A small line of change, in fact, different platforms have different implementations, why this, can be the world is diverse. Originally in the UNIX world to replace the line with/n instead, but windows in order to reflect his differences, the use of/r/n, more interesting is in the Mac with/R. So the UNIX series with the/n,windows series with the/r/n,mac with/R, so that you write the program on different platforms run with a lot of trouble. Here are some common ways to remove line breaks from PHP.

The first type of notation:

Copy CodeThe code is as follows: $content =str_replace ("\ n", "", $content);
Echo $content;



The second way:

Copy CodeThe code is as follows: Str_replace ("\ r \ n", "", $str);

The third type of notation:

Copy CodeThe code is as follows:
$content =preg_replace ("/\s/", "", $content);
Echo $content;

About \n,\r,\t
\ n Soft return: Represents a line break in Windows and returns to the beginning of the next line, in Linux, Unix, but not back to the beginning of the next line.
\ r Soft space: in Linux, Unix represents the return to the beginning of the line. Represents a newline in Mac OS and returns to the beginning of the next line, equivalent to the effect of \ n in Windows.
\ t jump (move to next column)

A few notes:

They are valid in a double-quote or delimiter-represented string, and are not valid in a single-quote string.
\ r \ n generally used together, to indicate the key on the keyboard return (Linux,unix), you can also only use \ n (windwos), in Mac OS with \ r to indicate carriage returns.
\ t represents the "tab" key on the keyboard.
NewLine symbol in file: Windows: \n,linux,unix: \ r \ n

Supplemental Code:

Copy CodeThe code is as follows:
<?php
PHP line breaks for different systems
The implementation of line breaks between different systems is not the same
Linux and Unix with/n
MAC with/R
Window is/r/n in order to show that it is different from Linux
So the implementation method on different platforms is not the same
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);
?>


Php_eol is a well-defined variable that represents the PHP newline character, which changes depending on the platform, is/r/n under Windows,/n under Linux, and/R under Mac. Line-up will be as follows.

Copy CodeThe code is as follows: $str = Str_replace (Php_eol, ", $STR);

PHP method Summary for removing line breaks (use of php_eol variables)

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.