Home >Backend Development >PHP Problem >How to remove carriage return and line feed in php
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.
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('//s*/', '', $str); //3、使用php定义好的变量 (建议使用) $str = str_replace(PHP_EOL, '', $str); ?>
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!