search
HomeBackend DevelopmentPHP ProblemHow to replace carriage return and line feed in php

How to replace carriage returns in php: 1. Use the statement "str_replace(array("/r", "/n", "/r/n"), "", $string);"; 2 , using regular expressions; 3. Through the statement "$str = str_replace(PHP_EOL,)".

How to replace carriage return and line feed in php

Three ways to replace carriage return and line feed in PHP

A small line break actually works on different platforms There are different implementations, why is this? The world is diverse!

Originally, /n is used to replace line breaks in the Unix world.

In order to reflect the difference, Windows uses /r/n.

What’s more interesting is that in Mac Used /r.

Therefore, there are a lot of troubles when writing programs on different platforms, such as replacing newlines.

The first way

str_replace(array("/r", "/n", "/r/n"), "", $string);

The second way is to use regular expressions

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

The third way

I have to look at it again here One of PHP's defined variables

PHP_EOL represents the newline character of PHP. This variable will change according to the platform. It will be /r/n under Windows and /n under Linux. Under mac it is /r

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

For more related articles, please visit PHP Chinese website!

The above is the detailed content of How to replace carriage return and line feed in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.