可以使用CodeIgniter输出压缩与< pre>显示代码块? [英] Possible to use CodeIgniter output compression with <pre> to display code blocks?

查看:100
本文介绍了可以使用CodeIgniter输出压缩与< pre>显示代码块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从此代码igniter压缩钩中排除< pre> 标记?我不明白正则表达式足够好,不能打破我的页面。

编辑:这个CodeIgniter Compression钩子会从​​代码中删除所有的单向空格和格式以便压缩输出。包括依靠该间距和格式显示代码的< pre> 标签。



尝试在压缩的输出页面中显示代码示例。

 <?php if(!defined('BASEPATH'))exit ('不允许直接脚本访问); 

function compress()
{
$ CI =& get_instance();
$ buffer = $ CI-> output-> get_output();

$ search = array(
'/ \\\
/',
'/ \> [^ \S] + / s',
'/ [^ \S] + \< / s',
'/(\s)+ / s'
);

$ replace = array(
'',
'>',
'<',
'\\1'
);

$ buffer = preg_replace($ search,$ replace,$ buffer);

$ CI-> output-> set_output($ buffer);
$ CI-> output-> _display();
}

?>


解决方案

让我们先看看你使用的代码现在。

  $ search = array(
'/ \\\
/',
'/ \> ; [^ \S] + / s',
'/ [^ \S] + \< / s',
'/(\s)+ / s'
);

$ replace = array(
'',
'>',
'<',
'\\1'
);

意图是将所有空格字符转换为简单空格,空间下降到一。除了可以通过第四个替换字符串中的 \\1 ,回车,制表符,换行符和其他空格字符滑过。



如果这个代码为你工作(除了< pre> 元素),这可能会工作,以及,如果不是更好:

  $ search ='/ ?> [^ \S] \s * | \s {2,})/`; 

$ replace ='';

现在我们可以添加一个先行来防止它匹配 <> 元素:

  $ search = 
' \ S] \s * | \s {2,})(?=(?:(?:[^<] ++ |<(?!/?pre\b))* (?:< pre> | \z))#`;

但实际上,这不是你正在做的工作的正确工具。我的意思是,看看这个怪物!



>请敦促您放弃此方法,而改用专用的HTML 缩小器,但一个似乎有自己的问题与< pre> 元素。如果这个问题已经修复,或者如果有另一个minifier满足你的需要,你一定要走这条路线。






EDIT:为回应评论,这里有一个排除< textarea> 以及< pre> 元素:

  $ search = 
'#(?ix)
[^ \S] \s * | \s {2,})
(?=
(?:(?:[^<] ++ | ?(?: textarea | pre)\b))* +)
(?:<(?> textarea | pre)\b | \z)

#'


Is it possible to exclude <pre> tags from this code igniter compression hook? I don't understand regular expressions well enough to not break my page. I have tried, but it always jacks up the output.

EDIT: This CodeIgniter Compression hook strips all unecisary white space and formatting from the code in order to compress the output. Including <pre> tags that rely on that spacing and formatting to display the code right.

I'm trying to show code examples in a compressed output page.

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function compress()
{
    $CI =& get_instance();
    $buffer = $CI->output->get_output();

     $search = array(
        '/\n/',
        '/\>[^\S ]+/s',
        '/[^\S ]+\</s',
        '/(\s)+/s'
      );

     $replace = array(
        ' ',
        '>',
        '<',
        '\\1'
      );

    $buffer = preg_replace($search, $replace, $buffer);

    $CI->output->set_output($buffer);
    $CI->output->_display();
}

?>

解决方案

Let's start by looking at the code you're using now.

 $search = array(
    '/\n/',
    '/\>[^\S ]+/s',
    '/[^\S ]+\</s',
    '/(\s)+/s'
  );

 $replace = array(
    ' ',
    '>',
    '<',
    '\\1'
  );

The intention appears to be to convert all whitespace characters to simple spaces, and to compress every run of multiple spaces down to one. Except it's possible for carriage-returns, tabs, formfeeds and other whitespace characters to slip through, thanks to the \\1 in the fourth replacement string. I don't think that's what the author intended.

If that code was working for you (aside from matching inside <pre> elements), this would probably work just as well, if not better:

$search = '/(?>[^\S ]\s*|\s{2,})/`;

$replace = ' ';

And now we can add a lookahead to prevent it from matching inside <pre> elements:

$search = 
  '#(?>[^\S ]\s*|\s{2,})(?=(?:(?:[^<]++|<(?!/?pre\b))*+)(?:<pre>|\z))#`;

But really, this is not the right tool for the job you're doing. I mean, look at that monster! You'll never be able to maintain it, and complicated as it is, it's still nowhere near as robust as it should be.

I was going to urge you to drop this approach and use a dedicated HTML minifier instead, but that one seems to have its own problems with <pre> elements. If that problem has been fixed, or if there's another minifier out there that would meet your needs, you should definitely go that route.


EDIT: In response to a comment, here's a version that excludes <textarea> as well as <pre> elements:

$search = 
  '#(?ix)
    (?>[^\S ]\s*|\s{2,})
    (?=
      (?:(?:[^<]++|<(?!/?(?:textarea|pre)\b))*+)
      (?:<(?>textarea|pre)\b|\z)
    )
    #'

这篇关于可以使用CodeIgniter输出压缩与&lt; pre&gt;显示代码块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆