How to remove newline characters in php?

青灯夜游
Release: 2023-03-06 07:26:01
Original
4257 people have browsed it

How to delete newline characters in php: 1. Use "str_replace(PHP_EOL, '', string)" to delete. "PHP_EOL" is a newline character variable defined in PHP; 2. Use "preg_replace ('//s*/', '', string)" to delete.

How to remove newline characters in php?

Recommended: "PHP Video Tutorial"

php Line breaks in different systems, and line breaks between different systems The implementation is different:

  • Used in linux and unix\n

  • Used in MAC\r

  • window In order to reflect the difference from Linux\r\n

So the implementation methods are different on different platforms. PHP has three ways to solve it

Method 1: Use str_replace to replace newlines

$str = str_replace(array("\r\n", "\r", "\n"), "", $str);
Copy after login

The str_replace() function replaces some characters in a string (case sensitive).

Method 2: Using regular replacement

$str = preg_replace('//s*/', '', $str);
Copy after login

preg_replace function performs a regular expression search and replacement.

Method 3: Use variables defined by php (recommended)

$str = str_replace(PHP_EOL, '', $str);
Copy after login

PHP_EOL is a defined variable that represents the newline character of php. This variable will It varies according to the platform. Under Windows, it will be /r/n, under Linux, it will be /n, and under Mac, it will be /r. Just press the following to change the line.

Line breaks in PHP can be replaced by PHP_EOL, which can improve the source code level portability of the code

Related recommendations: php training

The above is the detailed content of How to remove newline characters in php?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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