Home > Article > Backend Development > How to use regex to remove carriage return characters in php
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)".
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('/\r|\n/', '', $str); echo "\n去除回车符后:\n"; echo $str; ?>
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!