php 从 html 源中删除所有换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5258543/
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 all the line breaks from the html source
提问by mrN
Well I know obfuscation is a bad idea. But I want all of my html code to come in one long single line. All the html tags are generated through PHP, so I think its possible. I knew replacing \n\r
from regular expression, but have no idea how to do this one. In case I am unclear here is an example
好吧,我知道混淆是个坏主意。但我希望我所有的 html 代码都放在一个长单行中。所有的 html 标签都是通过 PHP 生成的,所以我认为这是可能的。我知道\n\r
从正则表达式替换,但不知道如何做这个。如果我不清楚这里是一个例子
$output = '<p>
<div class="title">Hello</div>
</p>';
echo $output;
To be view in the source viewer as <p><div class="title">Hello</div></p>
在源查看器中查看为 <p><div class="title">Hello</div></p>
回答by seriousdev
Maybe this?
也许这个?
$output = str_replace(array("\r\n", "\r"), "\n", $output);
$lines = explode("\n", $output);
$new_lines = array();
foreach ($lines as $i => $line) {
if(!empty($line))
$new_lines[] = trim($line);
}
echo implode($new_lines);
回答by Svish
You can try this perhaps.
也许你可以试试这个。
// Before any output
ob_start();
// End of file
$output = ob_get_clean();
echo preg_replace('/^\s+|\n|\r|\s+$/m', '', $output);
This should, unless I messed up the regex, catch all output, and then replace all new line characters as well as all whitespace at the end and beginning of lines.
这应该,除非我弄乱了正则表达式,捕获所有输出,然后替换所有新行字符以及行尾和行首的所有空格。
If you already have all output collected in a variable, you can of course just use the last line directly and skip the output buffering stuff :)
如果您已经将所有输出收集在一个变量中,您当然可以直接使用最后一行并跳过输出缓冲内容:)
回答by RayLoveless
Worked for me:
为我工作:
$output = str_replace(array("\r\n", "\r", "\n"), "", $output);
回答by krtek
You can do :
你可以做 :
$output = '<p>'.
'<div class="title">Hello</div>'.
'</p>';
This way, $output
won't contain any line jump.
这样,$output
不会包含任何行跳转。
This should also work :
这也应该有效:
$output = preg_replace(array('/\r/', '/\n/'), '', $output);
回答by ling
$output = preg_replace('!\s+!m', ' ', $output);
回答by Stephen Chung
This is already well answered, but you may be able to do more than just trim spaces at both ends of each line:
这已经得到了很好的回答,但您可以做的不仅仅是在每行两端修剪空格:
- First extract all text within quotes (you don't want to touch those), replace with a marker with a sequence number, store the sequence number with the text
- Extract all text within
<script></script>
tags and do the same as step #1 - Replace all white-space (including \n, \r) with spaces
- Replace all >1 space sequences with 1 space
- Replace all
>_<
with><
(_ = space) - Replace all
_>
,<_
and</_
with>
,<
and</
(_ = space) - Replace markers with the actual texts
- 首先提取引号内的所有文本(您不想触摸它们),用带有序列号的标记替换,将序列号与文本一起存储
- 提取
<script></script>
标签中的所有文本并执行与步骤 #1 相同的操作 - 用空格替换所有空格(包括\n、\r)
- 用 1 个空格替换所有 >1 个空格序列
- 全部替换
>_<
为><
(_ = 空格) - 用,和 (_ = 空格)替换所有
_>
,<_
and</_
>
<
</
- 用实际文本替换标记
This procedure can potentially compact the entire HTML file. This takes advantage of the fact that multiple white-space text inside HTML tags are intepreted as one single space.
此过程可能会压缩整个 HTML 文件。这利用了 HTML 标签内的多个空白文本被解释为一个空格这一事实。
回答by James Billingham
This is a (as far as I have tested) working implementation of Stephen Chung's instructions. I'm not entirely convinced by number five, but have included it anyway.
这是(据我测试过的)Stephen Chung 指令的工作实现。我并不完全相信第五点,但无论如何都包括它。
Put the things you want to protect in the protected_parts array. Do it in order that you want them protected. If the starting and ending bits are different (as they would be in HTML tags), separate them by using a comma.
把你想保护的东西放在 protected_parts 数组中。这样做是为了您希望它们受到保护。如果开始位和结束位不同(就像在 HTML 标签中一样),请使用逗号将它们分开。
Also, I've no idea if this is the most optimised way of doing this, but it works for me and seems reasonably fast. Feel free to improve, etc. (Let me know if you do too!)
另外,我不知道这是否是最优化的方法,但它对我有用并且看起来相当快。随意改进等(如果你也这样做,请告诉我!)
function MinifyHTML($str) {
$protected_parts = array("<pre>,</pre>", "\"", "'");
$extracted_values = array();
$i = 0;
foreach ($protected_parts as $part) {
$finished = false;
$search_offset = 0;
$first_offset = 0;
$startend = explode(",", $part);
if (count($startend) == 1) { $startend[1] = $startend[0]; }
while (!$finished) {
$first_offset = strpos($str, $startend[0], $search_offset);
if ($first_offset === false) { $finished = true; }
else {
$search_offset = strpos($str, $startend[1], $first_offset + strlen($startend[0]));
$extracted_values[$i] = substr($str, $first_offset + strlen($startend[0]), $search_offset - $first_offset - strlen($startend[0]));
$str = substr($str, 0, $first_offset + strlen($startend[0]))."$#".$i."$".substr($str, $search_offset);
$search_offset += strlen($startend[1]) + strlen((string)$i) + 3 - strlen($extracted_values[$i]);
$i++;
}
}
}
$str = preg_replace("/\s/", " ", $str);
$str = preg_replace("/\s{2,}/", " ", $str);
$str = str_replace("> <", "><", $str);
$str = str_replace(" >", ">", $str);
$str = str_replace("< ", "<", $str);
$str = str_replace("</ ", "</", $str);
for ($i = count($extracted_values); $i >= 0; $i--) {
$str = str_replace("$#".$i."$", $extracted_values[$i], $str);
}
return $str;
}
回答by piranxa
This is an improved function of the above. It adds text area protection and also anything that is a tag remains untouched.
这是上述功能的改进。它添加了文本区域保护,并且任何标记都保持不变。
I also removed strlen
in the loop (its static).
我也在strlen
循环中删除(它的静态)。
This might run faster as a one pass filter to check for any of the protected parts. For such a small protected_parts
array it's going to be more efficient than looping through the $str
four times.
这可能会作为单通过滤器运行得更快,以检查任何受保护的部件。对于这么小的protected_parts
数组,它比循环$str
四次更有效。
Also this doesn't fix: class = " " (the extra spaces between = and ") as its stuff inside the tags.
这也不能解决: class = " "(= 和 " 之间的额外空格)作为标签内的内容。
function MinifyHTML($str) {
$protected_parts = array('<pre>,</pre>','<textarea>,</textarea>', '<,>');
$extracted_values = array();
$i = 0;
foreach ($protected_parts as $part) {
$finished = false;
$search_offset = $first_offset = 0;
$end_offset = 1;
$startend = explode(',', $part);
if (count($startend) === 1) $startend[1] = $startend[0];
$len0 = strlen($startend[0]); $len1 = strlen($startend[1]);
while ($finished === false) {
$first_offset = strpos($str, $startend[0], $search_offset);
if ($first_offset === false) $finished = true;
else {
$search_offset = strpos($str, $startend[1], $first_offset + $len0);
$extracted_values[$i] = substr($str, $first_offset + $len0, $search_offset - $first_offset - $len0);
$str = substr($str, 0, $first_offset + $len0).'$$#'.$i.'$$'.substr($str, $search_offset);
$search_offset += $len1 + strlen((string)$i) + 5 - strlen($extracted_values[$i]);
++$i;
}
}
}
$str = preg_replace("/\s/", " ", $str);
$str = preg_replace("/\s{2,}/", " ", $str);
$replace = array('> <'=>'><', ' >'=>'>','< '=>'<','</ '=>'</');
$str = str_replace(array_keys($replace), array_values($replace), $str);
for ($d = 0; $d < $i; ++$d)
$str = str_replace('$$#'.$d.'$$', $extracted_values[$d], $str);
return $str;
}
回答by happy_marmoset
You can't have <div>
inside <p>
- it is not spec-valid.
你不能在<div>
里面<p>
- 它不是规范有效的。
If you don't need to store it in a variable you can use this:
如果您不需要将其存储在变量中,则可以使用以下命令:
?><div><?php
?><div class="title">Hello</div><?php
?></div><?php