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

How to use regex to remove carriage return characters in php

青灯夜游
Release: 2023-03-15 16:26:01
Original
2990 people have browsed it

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;
?>
Copy after login

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!

Related labels:
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