PHP删除多余的空格 [英] php removing excess whitespace

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

问题描述

我正在尝试从这样的字符串中删除多余的空格:

I'm trying to remove excess whitespace from a string like this:

你好     世界

hello        world

你好世界

任何人都知道如何在PHP中做到这一点吗?

Anyone has any idea how to do that in PHP?

推荐答案

使用正则表达式:

preg_replace('/( )+/', ' ', $string);

如果您还想删除所有多白色字符,则可以使用\ s(\ s是白色字符)

If you also want to remove every multi-white characters, you can use \s (\s is white characters)

preg_replace('/(\s)+/', ' ', $string);

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

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