php 从 HTML 中删除空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5362167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove whitespace from HTML
提问by James
I have HTML code like:
我有这样的 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 remove whitespace between tags by PHP?
如何通过 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>
采纳答案by Incognito
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.
我无法删除这个答案,但它不再相关,8 年来网络格局发生了巨大变化,以至于这已经变得毫无用处。
回答by Czechnology
$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.
但我看不出这有什么意义。如果您想减小数据大小,还有更好的选择。
回答by Savas Vedova
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
.
这些解决方案都不适合我,因此我想出了这个解决方案:使用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.
该函数ob_start
接受一个回调作为参数,该回调在输出之前应用于整个字符串。因此,如果您在刷新输出之前从字符串中删除空格,那么您就完成了。
/**
* Remove multiple spaces from the buffer.
*
* @var string $buffer
* @return string
*/
function removeWhitespace($buffer)
{
return preg_replace('/\s+/', ' ', $buffer);
}
ob_start('removeWhitespace');
<!DOCTYPE html>
<html>
<head></head>
<body></body>
</html>
ob_get_flush();
The above would print something like:
上面会打印出类似的东西:
<!DOCTYPE html> <html> <head> </head> <body> </body> </html>
Hope that helps.
希望有帮助。
HOW TO USE IT IN OOP
如何在 OOP 中使用它
If you're using object-orientated code in PHP you may want to use a call-back function that is inside an object.
如果您在 PHP 中使用面向对象的代码,您可能希望使用对象内部的回调函数。
If you have a class called, for instance HTML, you have to use this code line
如果您有一个名为的类,例如HTML,则必须使用此代码行
ob_start(["HTML","removeWhitespace"]);
回答by P.M
just in case someone needs this, I coined a function from @Martin Angelova's response and @Savas Vedova, and came up with
以防万一有人需要这个,我从@Martin Angelova 的回复和@Savas Vedova 中创造了一个函数,并提出了
<?php
function rmspace($buffer){
return preg_replace('~>\s*\n\s*<~', '><', $buffer);
};
?>
<?php ob_start("rmspace"); ?>
//Content goes in here
<?php ob_end_flush(); ?>
And it solved my problem. Note: I didn't test an server overhead, make sure you test before use in production
它解决了我的问题。注意:我没有测试服务器开销,请确保在用于生产之前进行测试
回答by Martin Angelov
$html = preg_replace('~>\s*\n\s*<~', '><', $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:
我认为这是解决<b>Hello</b> <i>world</i>
问题的方法。这个想法是只有在有新行时才删除空格。它将适用于常见的 HTML 语法,即:
<div class="wrap">
<div>
</div>
</div>
回答by laander
A RegEx replace could do the trick, something like:
RegEx 替换可以解决问题,例如:
$result = preg_replace('!\s+!smi', ' ', $content);
回答by Zeigen
The array reduce
function:
该array reduce
函数:
$html = explode("\n", $html);
function trimArray($returner, $value) {
$returner .= trim($value);
return $returner;
}
echo $html = array_reduce($html, 'trimArray');
回答by tfont
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
由于 gpupo 的帖子为许多不同类型的间距格式提供了最干净的解决方案。然而,最后却忘记了一个次要但重要的部分!最后的字符串修剪:-p
Below is a tested and working solution.
以下是经过测试且有效的解决方案。
function compress_html($content)
{
$i = 0;
$content = preg_replace('~>\s+<~', '><', $content);
$content = preg_replace('/\s\s+/', ' ', $content);
while ($i < 5)
{
$content = str_replace(' ', ' ', $content);
$i++;
}
return trim($content);
}
回答by Chris
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.:
感谢您发布这个问题。问题确实是处理某些环境中的空白错误。虽然正则表达式解决方案适用于一般情况,但为了快速破解,请删除前导空格并在每行末尾添加标签。PHP 在关闭 ?> 之后删除换行符。例如:
<ul><?php ?>
<li><a id="nav-questions" href="/questions">Questions</a></li><?php ?>
<li><a id="nav-tags" href="/tags">Tags</a></li><?php ?>
<li><a id="nav-users" href="/users">Users</a></li><?php ?>
<li><a id="nav-badges" href="/badges">Badges</a></li><?php ?>
<li><a id="nav-unanswered" href="/unanswered">Unanswered</a></li><?php ?>
</ul>
Obviously this is sub-optimal for a variety of reasons, but it'll work for a localized problem without affecting the entire tool chain.
显然,由于各种原因,这不是最佳选择,但它可以解决局部问题,而不会影响整个工具链。
回答by Eran Lipshtein
if you got 8 bit ASCII, is will remove them and keep the chars in range 128-255
如果您有 8 位 ASCII,则将删除它们并将字符保持在 128-255 范围内
$text = preg_replace('/[\x00-\x1F\xFF]/', " ", $text );
If you have a UTF-8 encoded string is will do the work
如果你有一个 UTF-8 编码的字符串就可以了
$text = preg_replace('/[\x00-\x1F\x7F]/u', '', $text);
for more information you have this link more information
欲了解更多信息,你有这个链接 更多信息
回答by gpupo
//...
public function compressHtml($content)
{
$content = preg_replace('~>\s+<~', '><', $content);
$content = preg_replace('/\s\s+/', ' ', $content);
$i = 0;
while ($i < 5) {
$content = str_replace(' ', ' ', $content);
$i++;
}
return $content;
}