ds1379551 2018-04-02 10:14
浏览 61

如何在Zend Framework 1.12中缩小HTML输出

As per the process mentioned here https://www.tkcodez.info/minify-html-output-via-php-code/. I have tried to inject one script in one of my layout files like this:

// Layout.phtml

<?php
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'
    );
    $buffer = preg_replace($search, $replace, $buffer);
    return $buffer;
}
ob_start("sanitize_output");
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php /*?><meta name="verify-v1" content="zXRjEY50qXN7Oxbt0tAIDegKmUk5nG32qLmAzMuZSw0=" /><?php */?>
<meta name="google-site-verification" content="J-w0iienSxG8y23RN-NYZ0oh4y9YBygvb-ealpBmBi0" />

<?php
$controllerName = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
$actionName = Zend_Controller_Front::getInstance()->getRequest()->getActionName();

$popupsessionTutor = new Zend_Session_Namespace('Popup_Session_Tutor');
$popupsessionCenter = new Zend_Session_Namespace('Popup_Session_Center');

?>

---- rest of the phtml file -----

But could not find my html view source minified. What else I need to do?

  • 写回答

1条回答 默认 最新

  • dooso0594 2018-04-13 13:03
    关注

    Since you're using ZF1 and assuming MVC and what you've posted is a view script, you could write a controller plugin that defines a dispatchLoopShutdown method which does the response modification you want.

    <?php
    class My_Controller_Plugin_Sanitize extends Zend_Controller_Plugin_Abstract
    {
        public function dispatchLoopShutdown()
        {
            $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'
            );
            $body = preg_replace($search, $replace, $this->getResponse()->getBody());
    
            $this->getResponse()->setBody($body);
    
            unset($body);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作