search
HomeBackend DevelopmentPHP ProblemHow to remove newline character from string in php

php method to remove line breaks in strings: 1. Use the "str_replace("n", '', $str);" method to remove line breaks; 2. Use "str_replace("rn", '', $str);" method to remove newlines; 3. Remove newlines through preg_replace.

How to remove newline character from string in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer.

How to remove line breaks in strings in PHP

In PHP, sometimes we need to filter line breaks in strings, such as the article in Tianya PHP Blog For the description information of the page, I directly intercepted the article content, filtered out the HTML symbols, and finally filtered out the line breaks.

The following is a summary of common methods and PHP codes for removing line breaks. In fact, what Tianya wants to recommend to everyone is a system constant [PHP_EOL].

// 第1种写法:
<?php
str_replace("n", &#39;&#39;, $str);
?>
// 第2种写法:
<?php
str_replace("rn", &#39;&#39;, $str);
?>
// 第3种写法:
<?php
preg_replace("/s/", &#39;&#39;, $str);
?>

The following is the relevant explanation:

First let’s talk about n,r,t

n Soft Enter:

In In Windows, it means a line break and returns to the beginning of the next line.

In Linux/unix, it only means a line break, but does not return to the beginning of the next line.

r Soft space:

In Linux/unix, it means returning to the beginning of the current line

In Mac OS, it means wrapping and returning to the beginning of the next line, which is equivalent to the effect of n in Windows

t Tab (move to the next column)

Supplementary notes:

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

rn are 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

The newline symbol in the file:

windows: n
linux/unix: rn

[Recommended learning: PHP video tutorial]

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

// 1)使用转义字符函数
<?php
$str = str_replace(array("/r/n", "/r", "/n"), &#39;&#39;, $str);
?>
// 2)使用正则表达式替换
<?php
$str = preg_replace(&#39;//s*/&#39;, &#39;&#39;, $str);
?>
// 3)使用PHP系统常量【推荐】
$str = str_replace(PHP_EOL, &#39;&#39;, $str);
?>

The above is the detailed content of How to remove newline character from string 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 Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

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.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.