删除<pre>以外的新行

Jon*_*nes 2 php

我想删除一些html(使用php)中的新行,除了<pre>标签,其中空格显然很重要.

smd*_*ger 9

可能是3年后,但是......以下代码将删除所有换行符和空格,因为它不在预标签之外.干杯!

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");
Run Code Online (Sandbox Code Playgroud)