preg_replace所有空格 [英] preg_replace all spaces

查看:173
本文介绍了preg_replace所有空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用下划线替换所有空格,但以下内容不起作用:

I'm trying to replace all spaces with underscores and the following is not working:

$id = "aa aa";
echo $id;
preg_replace('/\s+/', '_', $id);
echo $id;

打印

aa aaaa aa

推荐答案

函数preg_replace不会就地修改字符串.它返回带有替换结果的新字符串.您应将调用结果分配给$id变量:

The function preg_replace doesn't modify the string in-place. It returns a new string with the result of the replacement. You should assign the result of the call back to the $id variable:

$id = preg_replace('/\s+/', '_', $id);

这篇关于preg_replace所有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆