如何在 PHP 变量中用空格替换制表符?
$data
包含制表符、前导空格和多个空格。 我希望用空格替换所有制表符。 多个空格合并为一个空格,并删除前导空格。
事实上,可以将输入数据: 转换
[ asdf asdf asdf asdf ]
为输出数据:
[asdf asdf asdf asdf]
我该怎么做?
$data
contains tabs, leading spaces and multiple spaces. I wish to replace all tabs with a space. Multiple spaces with one single space, and remove leading spaces.
In fact somthing that would turn this input data:
[ asdf asdf asdf asdf ]
Into output data:
[asdf asdf asdf asdf]
How do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
修剪、用单个空格替换制表符和多余空格:
Trim, replace tabs and extra spaces with single spaces:
这会消除您的前导(和尾随)空格。
这会将一个或多个空间的任何集合变成一个空间。
这样就摆脱了你的标签。
That gets rid of your leading (and trailing) spaces.
That turns any collection of one or more spaces into just one space.
That gets rid of your tabs.
假设方括号不是字符串的一部分,并且您只是将它们用于说明目的,那么:
您也许可以使用单个正则表达式来完成此操作,但它将是一个相当复杂的正则表达式。 上面的内容更加简单。
注意:我还假设您不想将“AB\t\tCD”(\t 是制表符)替换为“AB CD”。
Assuming the square brackets aren't part of the string and you're just using them for illustrative purposes, then:
You might be able to do that with a single regex but it'll be a fairly complicated regex. The above is much more straightforward.
Note: I'm also assuming you don't want to replace "AB\t\tCD" (\t is a tab) with "AB CD".
这个答案完全从字面上理解了这个问题:它仅涉及空格和制表符。 诚然,OP可能还希望在修剪/压缩的内容中包含其他类型的空白,但让我们假设他想保留嵌入的 CR 和/或 LF 。
首先,让我们设置一些常量。 如果需要进行修改,这将易于理解和维护。 我添加了一些额外的空格,以便您可以更轻松地比较相同点和不同点。
我们使用
\A
和\Z
转义字符指定主题的开始和结束,而不是典型的面向行的^
和$
元字符。 这并不是因为在这种情况下,它们与“防御性”编程一样需要,如果S
的值发生变化以使将来需要它们。现在是秘密武器:我们将利用 preg_replace 的一些特殊语义,即(强调)
因此,我们使用模式数组和替换数组来代替模式字符串和替换字符串,这会导致多余的模式
L
和R
被修剪。This answer takes the question completely literally: it is only concerned with spaces and tabs. Granted, the OP probably also wants to include other kinds of whitespace in what gets trimmed/compressed, but let's pretend he wants to preserve embedded CR and/or LF.
First, let's set up some constants. This will allow for both ease of understanding and maintainability, should modifications become necessary. I put in some extra spaces so that you can compare the similarities and differences more easily.
We are using
\A
and\Z
escape characters to specify the beginning and end of the subject, instead of the typical^
and$
which are line-oriented meta-characters. This is not so much because they are needed in this instance as much as "defensive" programming, should the value ofS
change to make them needed in the future.Now for the secret sauce: we are going to take advantage of some special semantics of preg_replace, namely (emphasis added)
So instead of a pattern string and replacement string, we are using a pattern array and replacement array, which results in the extra patterns
L
andR
being trimmed.如果您也需要删除
。
In case you need to remove
too.
经过一番挫折后,我发现这是最好的解决方案,因为它还删除了两个字符长的不间断空格:
$data = html_entity_decode(str_replace(' ',' ',htmlentities($data )));
// 替换比 \s 更多的空格字符类型$data = trim(preg_replace('/\h/', ' ', $data));
请参阅 比利诺亚
After much frustration I found this to be the best solution, as it also removes non breaking spaces which can be two characters long:
$data = html_entity_decode(str_replace(' ',' ',htmlentities($data)));
// replaces more space character types than \s$data = trim(preg_replace('/\h/', ' ', $data));
See billynoah
只要使用这个正则表达式,
它就会用一个空格替换所有制表符和空格,
这里正则表达式中的符号
+
表示一次或多次,模式的意思是,只要有两个或多个空格,就用一个空格替换
Just use this regex
it will replace all tabs and spaces by one space,
here sign
+
in regex means one or more times,pattern means, that wherever there are two or more spaces, replace it by one space