Home  >  Article  >  Backend Development  >  How to use regex to remove carriage return characters in php

How to use regex to remove carriage return characters in php

青灯夜游
青灯夜游Original
2022-03-17 19:46:522960browse

In PHP, you can use the preg_replace() function with the regular expression "/\r|\n/" to remove carriage returns; you only need to execute the regular expression to search for carriage returns in the string , and replace it with a null character, the syntax is "preg_replace('/\r|\n/','',$str)".

How to use regex to remove carriage return characters in php

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

In PHP, you can use the preg_replace() function Use regular expressions to remove carriage returns (line feeds).

The preg_replace function performs a regular expression search and replace.

Just use the preg_replace function to perform a regular expression search for carriage return characters in the string and replace them with empty characters.

Implementation code:

<?php
header("Content-type:text/html;charset=utf-8");
$str="676
sdfhg
ju80
87";
echo "去除回车符前:\n";
echo $str;
$str = preg_replace(&#39;/\r|\n/&#39;, &#39;&#39;, $str); 
echo "\n去除回车符后:\n";
echo $str;
?>

How to use regex to remove carriage return characters in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to use regex to remove carriage return characters 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