删除多个空格 [英] Remove multiple whitespaces
本文介绍了删除多个空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从MySQL数据库中获取$row['message']
,我需要删除所有空白,例如\n
\t
等.
I'm getting $row['message']
from a MySQL database and I need to remove all whitespace like \n
\t
and so on.
$row['message'] = "This is a Text \n and so on \t Text text.";
应设置为:
$row['message'] = 'This is a Text and so on Text text.';
我尝试过:
$ro = preg_replace('/\s\s+/', ' ',$row['message']);
echo $ro;
,但不会删除\n
或\t
,仅删除单个空格.谁能告诉我该怎么做?
but it doesn't remove \n
or \t
, just single spaces. Can anyone tell me how to do that?
推荐答案
您需要:
$ro = preg_replace('/\s+/', ' ',$row['message']);
您使用的是\s\s+
,表示空格(空格,制表符或换行符)后跟一个或多个空格.这实际上意味着用单个空格替换两个或多个空格.
You are using \s\s+
which means whitespace(space, tab or newline) followed by one or more whitespace. Which effectively means replace two or more whitespace with a single space.
您想要的是用单个空格替换一个或多个空格,因此您可以使用模式\s\s*
或\s+
(推荐)
What you want is replace one or more whitespace with single whitespace, so you can use the pattern \s\s*
or \s+
(recommended)
这篇关于删除多个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文