Home > Backend Development > PHP Tutorial > PHP remove string newline character example sharing_PHP tutorial

PHP remove string newline character example sharing_PHP tutorial

WBOY
Release: 2016-07-13 10:37:31
Original
1015 people have browsed it

First way of writing:

Copy code The code is as follows:

str_replace(" n", '', $str);
?>

The second way of writing:

Copy the code The code is as follows:

str_replace(" rn", '', $str);
?>

The third way of writing:

Copy code The code is as follows:

preg_replace(" /s/", '', $str);
?>

Related explanations below:
First let’s talk about n, r, t
n soft carriage return:
means line break in Windows and returns to the beginning of the next line
in Linux/ In unix, it only means line break, but it will not return to the beginning of the next line.
r Soft space:
means return to the beginning of the current line in Linux/unix.
means line break and return in Mac OS. Go to the beginning of the next line, which is equivalent to the effect of n in Windows
t tab (move to the next column)
Additional note:
They are in the string represented by double quotes or delimiters Valid, not valid in strings represented by single quotes.
rn is generally used together to represent the Enter key on the keyboard (in Linux and Unix). You can also just use n (in Windows). In Mac OS, use r to represent Enter!
t represents the TAB key on the keyboard
Newline symbol in the file:
windows: n
linux/unix: rn

The following uses code to illustrate three common methods of removing line breaks in strings in PHP

1. Use the escape character function

Copy the code The code is as follows:

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

2. Use regular expressions to replace

Copy code The code is as follows:

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

3. It is recommended to use PHP system constants

Copy the code The code is as follows:

$ str = str_replace(PHP_EOL, '', $str);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735240.htmlTechArticleThe first way to write: Copy the code as follows: ?php str_replace("n", '', $str) ; ? The second way of writing: Copy the code. The code is as follows: ?php str_replace("rn", '', $str); ? The third way of writing: Copy the code...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template