如何从 CodeIgniter 的视图中替换制表符和换行符?
How to replace tab and new line from a view in CodeIgniter?
我正在使用 CI 开发网站。通常我们会像下面这样编写 HTML 代码:
<header>
<div class="banner">
<div class="container">
<div class="row">
<div class="col-xs-12">
<h1>Site Title</h1>
<h2>Sub Totle</h2>
<div class="logo"></div>
</div>
</div>
</div>
</div>
</header>
之后,我们使用 CI 加载视图,例如:
$this->load->view('view');
现在我的问题是,如何通过替换 views/view.php 文件中的所有制表符和新行来加载此视图,如下所示?
<header><div class="banner"><div class="container"><div class="row"><div class="col-xs-12 ac"><h1>Site Title</h1><h2>Sub Totle</h2><div class="logo"></div></div></div></div></div></header>
一句话,如何丑化 HTML CodeIgniter 视图的代码?
提前致谢。
转到 system/application/config/config.php
并检查挂钩是否已启用:
$config['enable_hooks'] = TRUE;
在 system/application/config/hooks.php
中声明新的钩子:
// compress output
$hook['display_override'][] = array(
'class' => '',
'function' => 'compress',
'filename' => 'compress.php',
'filepath' => 'hooks'
);
在system/application/hooks/compress.php
中添加钩子:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function compress()
{
$CI =& get_instance();
$buffer = $CI->output->get_output();
$search = array(
'/\n/', // replace end of line by a space
'/\>[^\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'
);
$buffer = preg_replace($search, $replace, $buffer);
$CI->output->set_output($buffer);
$CI->output->_display();
}
/* End of file compress.php */
/* Location: ./system/application/hooks/compress.php */
来源:http://jeromejaglale.com/doc/php/codeigniter_compress_html
或者,您可以从以下位置轻松安装此挂钩:https://github.com/johngerome/CodeIgniter-Minifyhtml-hooks#codeigniter-minifyhtml-hooks
我正在使用 CI 开发网站。通常我们会像下面这样编写 HTML 代码:
<header>
<div class="banner">
<div class="container">
<div class="row">
<div class="col-xs-12">
<h1>Site Title</h1>
<h2>Sub Totle</h2>
<div class="logo"></div>
</div>
</div>
</div>
</div>
</header>
之后,我们使用 CI 加载视图,例如:
$this->load->view('view');
现在我的问题是,如何通过替换 views/view.php 文件中的所有制表符和新行来加载此视图,如下所示?
<header><div class="banner"><div class="container"><div class="row"><div class="col-xs-12 ac"><h1>Site Title</h1><h2>Sub Totle</h2><div class="logo"></div></div></div></div></div></header>
一句话,如何丑化 HTML CodeIgniter 视图的代码?
提前致谢。
转到 system/application/config/config.php
并检查挂钩是否已启用:
$config['enable_hooks'] = TRUE;
在 system/application/config/hooks.php
中声明新的钩子:
// compress output
$hook['display_override'][] = array(
'class' => '',
'function' => 'compress',
'filename' => 'compress.php',
'filepath' => 'hooks'
);
在system/application/hooks/compress.php
中添加钩子:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function compress()
{
$CI =& get_instance();
$buffer = $CI->output->get_output();
$search = array(
'/\n/', // replace end of line by a space
'/\>[^\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'
);
$buffer = preg_replace($search, $replace, $buffer);
$CI->output->set_output($buffer);
$CI->output->_display();
}
/* End of file compress.php */
/* Location: ./system/application/hooks/compress.php */
来源:http://jeromejaglale.com/doc/php/codeigniter_compress_html
或者,您可以从以下位置轻松安装此挂钩:https://github.com/johngerome/CodeIgniter-Minifyhtml-hooks#codeigniter-minifyhtml-hooks