PHP安装Tidy扩展实现HTML的格式化

安装tidy

yum install libtidy libtidy-devel -y

# 注:以下命令中的71为php版本 如需要安装在php5.4版把71改成54即可
cd /www/server/php/71/src/ext/tidy
/www/server/php/71/bin/phpize
./configure --with-php-config=/www/server/php/71/bin/php-config
make && make install
echo "extension = tidy.so" >> /www/server/php/71/etc/php.ini
service php-fpm-71 restart

使用

<?php
$html = '<!DOCTYPE html><html><head><meta charset="utf-8"/><title>jQuery进度条加载插件</title><!-- The Stylesheets --><link href="assets/nprogress/nprogress.css" rel="stylesheet" /><link href="assets/css/style.css" rel="stylesheet" /><!--[if lt IE 9]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]--></head><body><!--下面是无用代码--><a style="display:none" href="http://www.baidu.com">百度</a><!--上面是无用代码--><div id="main"></div><a href="#" id="loadMore">Load More</a><script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script><script src="assets/nprogress/nprogress.js"></script><script src="assets/js/script.js"></script></body></html>';
$result = beautify_html($html);
echo $result;
# HTML 格式化
function beautify_html($html){
    $tidy_config = array(
        'clean' => false,
        'indent' => true,
        'indent-spaces' => 4,
        'output-xhtml' => false,
        'show-body-only' => false,
        'wrap' => 0
    );
    if(function_exists('tidy_parse_string')){
        $tidy = tidy_parse_string($html, $tidy_config, 'utf8');
        $tidy -> cleanRepair();
        return $tidy;
    }
    else return $html;
}

格式化前:

<!DOCTYPE html><html><head><meta charset="utf-8"/><title>jQuery进度条加载插件</title><!-- The Stylesheets --><link href="assets/nprogress/nprogress.css" rel="stylesheet" /><link href="assets/css/style.css" rel="stylesheet" /><!--[if lt IE 9]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]--></head><body><!--下面是无用代码--><a style="display:none" href="http://www.baidu.com">百度</a><!--上面是无用代码--><div id="main"></div><a href="#" id="loadMore">Load More</a><script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script><script src="assets/nprogress/nprogress.js"></script><script src="assets/js/script.js"></script></body></html>

格式化后:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>
            jQuery进度条加载插件
        </title><!-- The Stylesheets -->
        <link href="assets/nprogress/nprogress.css" rel="stylesheet">
        <link href="assets/css/style.css" rel="stylesheet"><!--[if lt IE 9]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
    </head>
    <body>
        <!--下面是无用代码--><a style="display:none" href="http://www.baidu.com">百度</a><!--上面是无用代码-->
        <div id="main"></div><a href="#" id="loadMore">Load More</a>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script src="assets/nprogress/nprogress.js"></script>
        <script src="assets/js/script.js"></script>
    </body>
</html>

更多设置选项

http://api.html-tidy.org/tidy/tidylib_api_5.2.0/tidy_config.html

关于删除空标签的解决办法

很多类似于<em></em>这样的空标签默认会被删除,如果需要保留,可以设置:

'drop-empty-elements' => false

发表评论

邮箱地址不会被公开。 必填项已用*标注