Php 删除新行,但在<;预处理>;
我想从一些html(使用php)中删除新行,但在空白明显重要的标记中除外。如果html格式正确,您可以依赖这样一个事实,Php 删除新行,但在<;预处理>;,php,Php,我想从一些html(使用php)中删除新行,但在空白明显重要的标记中除外。如果html格式正确,您可以依赖这样一个事实,标记不允许嵌套。进行两次传递:首先,将输入拆分为pre标记块和其他所有内容。您可以为此任务使用正则表达式。然后从每个非预块中剥离新线,最后将它们重新连接在一起 <?php //out example HTML file - could just as easily be a read in file $html = <<<EOF <html>
标记不允许嵌套。进行两次传递:首先,将输入拆分为pre标记块和其他所有内容。您可以为此任务使用正则表达式。然后从每个非预块中剥离新线,最后将它们重新连接在一起
<?php
//out example HTML file - could just as easily be a read in file
$html = <<<EOF
<html>
<head>
<title>test</title>
</head>
<body>
<h1>Title</h1>
<p>
This is an article about...
</p>
<pre>
line one
line two
line three
</pre>
<div style="float: right:">
random
</div>
</body>
</html>
EOF;
//break it all apart...
$blocks = preg_split('/<(|\/)pre>/', $html);
//and put it all back together again
$html = ""; //reuse as our buffer
foreach($blocks as $i => $block)
{
if($i % 2 == 1)
$html .= "\n<pre>$block</pre>\n"; //break out <pre>...</pre> with \n's
else
$html .= str_replace(array("\n", "\r"), "", $block, $c);
}
echo $html;
?>
请注意,大多数html的格式都不好,因此这种方法可能会限制您使用它的位置。将内容拆分。这是很容易做到的与
function sanitize_output($buffer)
{
$search = array(
'/\>[^\S ]+/s', //strip whitespaces after tags, except space
'/[^\S ]+\</s', //strip whitespaces before tags, except space
'/(\s)+/s' // shorten multiple whitespace sequences
);
$replace = array(
'>',
'<',
'\\1'
);
$blocks = preg_split('/(<\/?pre[^>]*>)/', $buffer, null, PREG_SPLIT_DELIM_CAPTURE);
$buffer = '';
foreach($blocks as $i => $block)
{
if($i % 4 == 2)
$buffer .= $block; //break out <pre>...</pre> with \n's
else
$buffer .= preg_replace($search, $replace, $block);
}
return $buffer;
}
ob_start("sanitize_output");
随机的
EOF;
//把它全部拆开。。。
$blocks=preg_split('/',$html);
//然后把它们重新组合起来
$html=“”//重用为缓冲区
foreach($i=>$block的块)
{
如果($i%2==1)
$html.=“\n$block\n”;//用\n的
其他的
$html.=str_replace(数组(“\n”,“\r”),“,$block,$c);
}
echo$html;
?>
可能是3年后,但是。。。以下代码将删除所有换行符和空格,只要它在pre标记之外。干杯
function sanitize\u输出($buffer)
{
$search=array(
'/\>[^\S]+/S',//除去标记后的空格,空格除外
“/[^\S]+\”,
“)/”,$buffer,null,PREG_SPLIT_DELIM_CAPTURE);
$buffer='';
foreach($i=>$block的块)
{
如果($i%4==2)
$buffer.=$block;//用\n的
其他的
$buffer.=preg_replace($search,$replace,$block);
}
返回$buffer;
}
ob_启动(“消毒_输出”);
这本质上是html缩小,这是另一篇文章的主题:。
function sanitize_output($buffer)
{
$search = array(
'/\>[^\S ]+/s', //strip whitespaces after tags, except space
'/[^\S ]+\</s', //strip whitespaces before tags, except space
'/(\s)+/s' // shorten multiple whitespace sequences
);
$replace = array(
'>',
'<',
'\\1'
);
$blocks = preg_split('/(<\/?pre[^>]*>)/', $buffer, null, PREG_SPLIT_DELIM_CAPTURE);
$buffer = '';
foreach($blocks as $i => $block)
{
if($i % 4 == 2)
$buffer .= $block; //break out <pre>...</pre> with \n's
else
$buffer .= preg_replace($search, $replace, $block);
}
return $buffer;
}
ob_start("sanitize_output");