从字符串中删除所有空白字符
我知道 此评论 PHP.net< /em>。
我想要一个类似的工具,例如 tr
对于 PHP,这样我就可以简单地运行
tr -d " " ""
我无法成功运行函数 php_strip_whitespace
$tags_trimmed = php_strip_whitespace($tags);
我运行正则表达式函数也失败了
$tags_trimmed = preg_replace(" ", "", $tags);
I know this comment on PHP.net.
I would like to have a similar tool like tr
for PHP such that I can run simply
tr -d " " ""
I run unsuccessfully the function php_strip_whitespace
by
$tags_trimmed = php_strip_whitespace($tags);
I run the regex function also unsuccessfully by
$tags_trimmed = preg_replace(" ", "", $tags);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
要去除任何空格,您可以使用正则表达式
另请参阅此答案了解可以的内容处理 UTF-8 字符串中的空格。
To strip any whitespace, you can use a regular expression
See also this answer for something which can handle whitespace in UTF-8 strings.
默认情况下,正则表达式不考虑 UTF-8 字符。
\s
元字符仅说明原始拉丁语集。 因此,以下命令仅删除制表符、空格、回车符和换行符。随着 UTF-8 成为主流,该表达式在到达新的 UTF-8 字符时将更频繁地失败/停止,从而在
\ 后面留下空格s
无法解释。为了处理 Unicode/UTF-8 中引入的新型空白,需要更广泛的字符串来匹配和删除现代空白。
由于正则表达式默认不识别多字节字符,因此只能使用定界元字符串来识别它们,以防止字节段被其他 utf-8 字符(
\x80
四元组可以替换智能引号中的所有\x80
子字节)这会考虑并删除制表符、换行符、垂直制表符、换页符、回车符、空格以及 此处:
当从自动化工具或站点导出时,其中许多会对 XML 文件造成严重破坏,这些文件会扰乱文本搜索和识别,并且可以不可见地粘贴到 PHP 源代码中,从而导致解析器跳转到下一个命令(段落和行分隔符),这会导致代码行被跳过,从而导致间歇性的、无法解释的错误,我们已经开始将其称为“文本传播疾病”
(从网络复制和粘贴不再安全。使用字符扫描仪来保护您的代码。哈哈)
A regular expression does not account for UTF-8 characters by default. The
\s
meta-character only accounts for the original Latin set. Therefore, the following command only removes tabs, spaces, carriage returns and new linesWith UTF-8 becoming mainstream this expression will more frequently fail/halt when it reaches the new UTF-8 characters, leaving white spaces behind that the
\s
cannot account for.To deal with the new types of white spaces introduced in Unicode/UTF-8, a more extensive string is required to match and removed modern white space.
Because regular expressions by default do not recognize multi-byte characters, only a delimited meta string can be used to identify them, to prevent the byte segments from being alters in other utf-8 characters (
\x80
in the quad set could replace all\x80
sub-bytes in smart quotes)This accounts for and removes tabs, newlines, vertical tabs, formfeeds, carriage returns, spaces, and additionally from here:
Many of these wreak havoc in XML files when exported from automated tools or sites which foul up text searches, recognition, and can be pasted invisibly into PHP source code which causes the parser to jump to next command (paragraph and line separators) which causes lines of code to be skipped resulting in intermittent, unexplained errors that we have begun referring to as "textually transmitted diseases"
(It's not safe to copy and paste from the web anymore. Use a character scanner to protect your code. lol)
有时您需要删除连续的空格。 你可以这样做:
输出:
Sometimes you would need to delete consecutive white spaces. You can do it like this:
Output:
我相信 preg_replace 会寻找类似
[:space:]
的内容I believe preg_replace would be looking for something like
[:space:]
您可以使用 php 中的 trim 函数来修剪两侧(左侧和右侧) )
或者
您也可以使用
系统: PHP 4,5,7
You can use the trim function from php to trim both sides (left and right)
Or
You can also use
System: PHP 4,5,7
如果您想从 $tags 中删除所有空格,只需使用:
如果您想删除新行等,则需要更多...
If you want to remove all whitespaces everywhere from $tags, just use:
If you want to remove new lines and such that would require a bit more...
任何可能的选择是使用自定义文件包装器将变量模拟为文件。 您可以通过使用以下方法来实现:
1)首先,注册您的包装器(仅在文件中注册一次,像 session_start() 一样使用它):
2)然后定义您的包装器类(它写得非常快,不完全正确,但是它有效):
3)然后在 var:// 协议上将任何文件函数与包装器一起使用(您也可以将其用于 include、require 等):
注意:不要忘记将变量置于全局范围内(例如全局变量) $__myVar)
Any possible option is to use custom file wrapper for simulating variables as files. You can achieve it by using this:
1) First of all, register your wrapper (only once in file, use it like session_start()):
2) Then define your wrapper class (it is really fast written, not completely correct, but it works):
3) Then use any file function with your wrapper on var:// protocol (you can use it for include, require etc. too):
Note: Don't forget to have your variable in global scope (like global $__myVar)
最短的答案:
strtr($str, [' '=>'']);
另一种“剥皮这只猫”的常见方法是使用爆炸和内爆,如下所示:
implode ('', 爆炸(' ', $str));
The shortest answer:
strtr($str, [' '=>'']);
Another common way to "skin this cat" would be to use explode and implode like this:
implode('', explode(' ', $str));
基于这个被视为空格的字符表,简洁的模式可以使用
\pZ
和\pC
消除所有空格和控制字符。 这是这些正则表达式元字符的一个很好的参考。匹配正则表达式模式中所有空格的详细方法如下所示:
演示
/ \s+/
/\s+/u
/\pZ+/u
/\pC+/u
/[\pZ \pC]+/u
Based on this table of characters that are considered whitespaces, a concise pattern can use
\pZ
and\pC
to eliminate all of the whitespace and control characters. Here is a great reference to these regex metacharacters.The verbose way of matching all of whitespaces in a regex pattern can look like this:
Demo
/\s+/
/\s+/u
/\pZ+/u
/\pC+/u
/[\pZ\pC]+/u
您还可以使用 preg_replace_callback() 函数。 该函数与其同级函数 preg_replace() 相同,除了它可以采用回调函数之外,它可以让您更好地控制如何操作输出。
You could also use the preg_replace_callback() function. And this function is identical to its sibling preg_replace(), except for it can take a callback function which gives you more control on how you manipulate your output.
您可以使用
ereg_replace
来完成此操作对于最新版本,您可以使用
By using ASCII
You can do it by using
ereg_replace
For the newest version, you can use
By using ASCII
从整个字符串中删除空格的一个简单方法是使用explode函数并使用for循环打印整个字符串。
A simple way to remove spaces from the whole string is to use the explode function and print the whole string using a for loop.
标签形式有一些特殊类型的空白。
您需要使用
$str=strip_tags($str);
删除多余标签、错误标签,以首先获得正常字符串。
并使用
$str=preg_replace('/\s+/', '', $str);
这对我有用。
There are some special types of whitespace in the form of tags.
You need to use
$str=strip_tags($str);
to remove redundant tags, error tags, to get to a normal string first.
And use
$str=preg_replace('/\s+/', '', $str);
It's work for me.
可以这样完成:
标准的 trim() 函数可以当 HTML 实体出现时就会出现问题。 这就是为什么我写了一个“Super Trim”函数来处理这个问题,你也可以选择从字符串的开头、结尾或两侧进行修剪。
It can be done like this:
The standard trim() functions can be a problematic when come HTML entities. That's why I wrote a "Super Trim" function what is used to handle this problem and also you can choose is trimming from the beginning, end or both sides of the string.
\s 正则表达式参数与每个系统上的所有 UTF-8 多字节字符串不兼容,即使与 /u 配对也是如此。
注意:这种方法是我几年前提出的,当时我尝试了当时发布的所有其他方法,发现它们不适用于我当时工作中使用的平台以及提供给我的数据集。 如果其他人发现自己处于常见答案不适用的情况,也许这个答案可能会有所帮助。
这个 PHP 正则表达式是我为了解决这个问题而编写的,它使用基于 PCRE (Perl 兼容正则表达式)参数作为 UTF-8 字符串的替换:
- 用法示例 -
之前:
结果:
并且:
之后:
结果:
并且:
PCRE 参数列表
来源:https://www.rexegg.com/regex-quickstart .html
The \s regex argument is not compatible with all UTF-8 multi-byte strings on every system even when paired with /u.
NOTE: This method I came up with years ago after attempting all other methods posted here at the time and finding they did not work on the platform used at my job back then with the data set that was provided to me. If someone else finds themselves in a situation where the common answers don't apply, perhaps maybe this one might help.
This PHP regular expression is one I wrote to solve this using PCRE (Perl Compatible Regular Expressions) based arguments as a replacement for UTF-8 strings:
- Example Usage -
Before:
Result:
And:
After:
Result:
And:
PCRE Argument Listing
Source: https://www.rexegg.com/regex-quickstart.html