当前位置:首页 > 建站笔记 > 正文

php怎么利用正则去掉回车符

在php中,可以使用preg_replace()函数配合正则表达式“/\r|\n/”来去掉回车符;只需要执行该正则表达式搜索字符串中的回车符,并将其替换为空字符即可,语法“preg_replace('/\r|\n/','',$str)”。

本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑

在php中,可以使用preg_replace()函数配合正则表达式来去掉回车符(换行)。

preg_replace 函数执行一个正则表达式的搜索和替换。

只需要使用preg_replace 函数执行正则表达式搜索字符串中的回车符,并将其替换为空字符即可。

实现代码:

<?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;
?>

发表评论