How to remove carriage return and line feed in php

藏色散人
Release: 2023-03-10 15:08:02
Original
3057 people have browsed it

How to remove carriage returns and line feeds in php: 1. Use str_replace to replace carriage returns and line feeds; 2. Use regular expressions to replace carriage returns and line feeds; 3. Use variables defined by php to remove carriage returns and line feeds.

How to remove carriage return and line feed in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

Three ways to remove line feed (carriage return and line feed) in php Method

The code is as follows:

<?php   
 //php 不同系统的换行  
//不同系统之间换行的实现是不一样的  
//linux 与unix中用 \n  
//MAC 用 \r  
//window 为了体现与linux不同 则是 \r\n  
//所以在不同平台上 实现方法就不一样  
//php 有三种方法来解决  
//1、使用str_replace 来替换换行  
$str = str_replace(array("\r\n", "\r", "\n"), "", $str);   
//2、使用正则替换  
$str = preg_replace(&#39;//s*/&#39;, &#39;&#39;, $str);  
//3、使用php定义好的变量 (建议使用)  
$str = str_replace(PHP_EOL, &#39;&#39;, $str);   
?>
Copy after login

Related introduction:

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

This function must follow the following rules:

If the searched string is an array, then it will return an array. If the string being searched is an array, then it will find and replace each element in the array. If an array needs to be searched and replaced at the same time, and the elements to be replaced are less than the number of elements found, the excess elements will be replaced with empty strings. If you search an array and replace only one string, the replacement string will apply to all found values.

Note: This function is case-sensitive. Please use the str_ireplace() function to perform a case-insensitive search.

Note: This function is binary safe.

Recommended learning: "PHP Video Tutorial"

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

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