Example of removing string line breaks in php

Source: Internet
Author: User

Example of removing string line breaks in php

This article mainly introduces the example of removing string line breaks in php. The following describes common methods for removing line breaks. For more information, see

1st writing methods:

The Code is as follows:

<? Php

Str_replace ("n", '', $ str );

?>

 

 

2nd writing methods:

The Code is as follows:

<? Php

Str_replace ("rn", '', $ str );

?>

 

 

3rd writing methods:

 

The Code is as follows:

<? Php

Preg_replace ("/s/", '', $ str );

?>

 

 

The following is a description:

First, let's talk about n, r, t

N soft carriage return:

In Windows, line breaks are displayed and returned to the beginning of the next line.

In Linux/unix, it only indicates line breaks, but does not return to the starting position of the next line.

R Soft Space:

In Linux/unix, return to the starting position of the row

In Mac OS, line breaks are represented and returned to the start position of the next line, which is equivalent to n in Windows.

T-hop (move to the next column)

Note:

They are valid in double quotes or delimiters, and are invalid in single quotes.

Rn is generally used together to represent the Enter key on the keyboard (in Linux and Unix). You can also use n (in Windwos) and press enter in Mac OS!

T indicates the TAB key on the keyboard.

Line feed characters in the file:

Windows: n

Linux/unix: rn

 

The following code demonstrates three common methods for removing line breaks from strings in PHP:

 

1. Use escape character Functions

 

The Code is as follows:

<? Php

$ Str = str_replace (array ("/r/n", "/r", "/n"), '', $ str );

?>

 

 

2. replace with a regular expression

 

The Code is as follows:

<? Php

$ Str = preg_replace ('// s */', '', $ str );

?>

 

 

3. PHP system constants are recommended.

 

The Code is as follows:

<? Php

$ Str = str_replace (PHP_EOL, '', $ str );

?>

 

 

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.