删除 HTML 中的空格
我有这样的 HTML 代码:
<div class="wrap">
<div>
<div id="hmenus">
<div class="nav mainnavs">
<ul>
<li><a id="nav-questions" href="/questions">Questions</a></li>
<li><a id="nav-tags" href="/tags">Tags</a></li>
<li><a id="nav-users" href="/users">Users</a></li>
<li><a id="nav-badges" href="/badges">Badges</a></li>
<li><a id="nav-unanswered" href="/unanswered">Unanswered</a></li>
</ul>
</div>
</div>
</div>
</div>
How do I reremoving space Between labels by PHP?
我们应该得到:
<div class="wrap"><div><div id="hmenus"><div class="nav mainnavs"><ul><li><a id="nav-questions" href="/questions">Questions</a></li><li><a id="nav-tags" href="/tags">Tags</a></li><li><a id="nav-users" href="/users">Users</a></li><li><a id="nav-badges" href="/badges">Badges</a></li><li><a id="nav-unanswered" href="/unanswered">Unanswered</a></li></ul></div></div></div></div>
I have HTML code like:
<div class="wrap">
<div>
<div id="hmenus">
<div class="nav mainnavs">
<ul>
<li><a id="nav-questions" href="/questions">Questions</a></li>
<li><a id="nav-tags" href="/tags">Tags</a></li>
<li><a id="nav-users" href="/users">Users</a></li>
<li><a id="nav-badges" href="/badges">Badges</a></li>
<li><a id="nav-unanswered" href="/unanswered">Unanswered</a></li>
</ul>
</div>
</div>
</div>
</div>
How do I remove whitespace between tags by PHP?
We should get:
<div class="wrap"><div><div id="hmenus"><div class="nav mainnavs"><ul><li><a id="nav-questions" href="/questions">Questions</a></li><li><a id="nav-tags" href="/tags">Tags</a></li><li><a id="nav-users" href="/users">Users</a></li><li><a id="nav-badges" href="/badges">Badges</a></li><li><a id="nav-unanswered" href="/unanswered">Unanswered</a></li></ul></div></div></div></div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
$html = preg_replace('~>\s+<~', '><', $html);
但我不明白这有什么意义。如果您想减小数据大小,还有更好的选择。
$html = preg_replace('~>\s+<~', '><', $html);
But I don't see the point of this. If you're trying to make the data size smaller, there are better options.
自从第一次问这个问题以来已经有一段时间了,但我仍然认为有必要发布这个答案以帮助遇到同样问题的人。
这些解决方案都不适合我,因此我想出了这个解决方案:使用
output_buffer
。函数 ob_start 接受回调作为参数,该参数在输出之前应用于整个字符串。因此,如果您在刷新输出之前从字符串中删除空格,那么您就完成了。
上面的内容会打印出类似这样的内容:
希望有帮助。
如何在 OOP 中使用它
如果您在 PHP 中使用面向对象的代码,您可能需要使用对象内部的回调函数。
如果您有一个名为 HTML 的类,则必须使用此代码行
It's been a while since this question was first asked but I still see the need to post this answer in order to help people with the same problem.
None of these solutions were adoptabe for me therefore I've came up with this solution: Using
output_buffer
.The function
ob_start
accepts a callback as an argument which is applied to the whole string before outputting it. Therefore if you remove whitespace from the string before flushing the output, there you're done.The above would print something like:
Hope that helps.
HOW TO USE IT IN OOP
If you're using object-orientated code in PHP you may want to use a call-back function that is inside an object.
If you have a class called, for instance HTML, you have to use this code line
以防万一有人仍然需要这个,我从 @Martin Angelova 的回复和 @Savas Vedova 中创造了一个函数,结果也解决了我的问题:
注意:我没有在生产环境中测试性能损失
Just in case someone still needs this, I coined a function from @Martin Angelova's response and @Savas Vedova, the outcome that also solved my problem looks:
Note: I did not test the performance penalty in a production environment
正则表达式替换可以解决这个问题,例如:
A RegEx replace could do the trick, something like:
我认为这是
Hello 的解决方案世界
问题。这个想法是仅当有新行时才删除空格。它将适用于常见的 HTML 语法,即:I'm thinking that this is the solution to the
<b>Hello</b> <i>world</i>
problem. The idea is to remove whitespace only when there's a new line. It will work for common HTML syntax which is:感谢您发布这个问题。问题确实是处理某些环境中的空白错误。虽然正则表达式解决方案适用于一般情况,但为了快速破解,请删除前导空格并将标签添加到每行末尾。 PHP 删除结束 ?> 之后的换行符。例如:
显然,由于多种原因,这不是最优的,但它可以解决局部问题,而不会影响整个工具链。
Thank you for posting this question. The problem is indeed dealing with whitespace bugs in certain environments. While the regex solution works in the general case, for a quick hack remove leading whitespace and add tags to the end of each line. PHP removes the newline following a closing ?>. E.g.:
Obviously this is sub-optimal for a variety of reasons, but it'll work for a localized problem without affecting the entire tool chain.
数组缩减
函数:The
array reduce
function:由于 gpupo 的帖子为许多不同类型的间距格式提供了最干净的解决方案。然而,最后却忘记了一个虽小但重要的部分!最终的字符串修剪:-p
下面是经过测试且有效的解决方案。
As gpupo's post provided the cleanest solution for many different types of spacing formatting's. However, a minor but important piece was forgotten at the end! A final string trim :-p
Below is a tested and working solution.
这对我有用,并且很容易添加/删除特殊情况。适用于 CSS、HTML 和 JS。
This works for me and it's easy to add/remove special cases. Works with CSS, HTML and JS.
如果您有 8 位 ASCII,则将删除它们并将字符保留在 128-255 范围内
如果您有 UTF-8 编码字符串,则将完成
更多信息的 工作
你有这个链接
更多信息
if you got 8 bit ASCII, is will remove them and keep the chars in range 128-255
If you have a UTF-8 encoded string is will do the work
for more information
you have this link
more information
使用正则表达式,例如:
Use regular expressions, like:
我使用了这个正则表达式,它的工作原理就像一个魅力:
这些模式查找空格和制表符(至少一个),即后面不跟着
"
或'
,这是为了避免删除 html 属性之间的空格。I used this regex for me and it works like a charm:
These pattern looks for space whitespace and tabulator (at least one), that is not followed by
"
or'
. This is, to avoid removing whitespaces between html attributes.我无法删除这个答案,但它不再相关了,网络格局在 8 年内发生了很大变化,以至于它变得毫无用处。
I can't delete this answer but it's no longer relevant, the web landscape has changed so much in 8 years that this has become useless.