自动缩进HTML输出?

3
我希望能自动缩进PHP脚本输出的HTML。我使用HTML Purifier进行内部文本框输入表单验证,并考虑使用HTMLTidy。然而,由于HTML purifier依赖于HTML Tidy,后者没有得到积极开发并且有很多错误报告,因此我正在寻找其他解决方案。我已经找到了:

https://github.com/gajus/dindent

我应该选择这个吗?- 是否有其他脚本或方法来处理输出?

目前,我正在以一种相当费力的方式进行缩进,例如(函数的一部分,需要缩进参数):

echo str_repeat("\t", $indentation) . "<ul>\n";
foreach($result as $value)
    {
    echo str_repeat("\t", $indentation) . "\t<li>" . datetime_converter($value["Date"]) . " - " . $value["Article"] . "</li>\n";
    }
echo str_repeat("\t", $indentation) . "</ul>";

谢谢!


外部脚本注释仅用于说明我已经做好了功课!- 我收到了一个很好的回复,我只需要让它工作起来,所以请解锁它。 - mrmut
以下脚本解决了问题!! - mrmut
1个回答

3
这是我的缩进函数,希望能对你有所帮助。
class Html {
    private static $_indent = "  ";

    private static function indentStr($indentlevel = 0){
        $replaceindent = null;

        //Sets the indentation from current indentlevel
        for($o = 0; $o < $indentlevel; $o++) {
            $replaceindent .= self::$_indent;
        }

        return $replaceindent;
    }

    public static function indent($uncleanhtml) {   
        // Seperate tags
        $uncleanhtml_array = explode("<", $uncleanhtml);
        $uncleanhtml_array = array_filter($uncleanhtml_array);
        foreach($uncleanhtml_array as $unfixedtextkey => $unfixedtextvalue) {
            if(!trim($unfixedtextvalue)){
                continue;
            }

            $unfixedtextvalue = '<' . trim($unfixedtextvalue);

            //Makes sure empty lines are ignores
            if(!preg_match("/^(\s)*$/", $unfixedtextvalue)) {
                $fixedtextvalue = preg_replace("/>(\s|\t)*</U", ">\n<", $unfixedtextvalue);
                $uncleanhtml_array[$unfixedtextkey] = $fixedtextvalue;
            }

        }

        //Sets no indentation
        $indentlevel = 0;
        foreach($uncleanhtml_array as $uncleanhtml_key => $currentuncleanhtml) {
            //Removes all indentation
            $currentuncleanhtml = preg_replace("/\t+/", "", $currentuncleanhtml);
            $currentuncleanhtml = preg_replace("/^\s+/", "", $currentuncleanhtml);

            $replaceindent = self::indentStr($indentlevel);

            //If self-closing tag, simply apply indent
            if(preg_match("/<(.+)\/>/", $currentuncleanhtml)) { 
                $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
            } else if(preg_match("/<!(.*)>/", $currentuncleanhtml)) { 
                //If doctype declaration, simply apply indent
                $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
            } else if(preg_match("/<[^\/](.*)>/", $currentuncleanhtml) && preg_match("/<\/(.*)>/", $currentuncleanhtml)) {
                //If opening AND closing tag on same line, simply apply indent 
                $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
            } else if(preg_match("/<\/(.*)>/", $currentuncleanhtml) || preg_match("/^(\s|\t)*\}{1}(\s|\t)*$/", $currentuncleanhtml)) {
                //If closing HTML tag or closing JavaScript clams, decrease indentation and then apply the new level
                $indentlevel--;
                $replaceindent = self::indentStr($indentlevel);

                $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
            } else if((preg_match("/<[^\/](.*)>/", $currentuncleanhtml) && !preg_match("/<(link|meta|base|br|img|hr|\?)(.*)>/", $currentuncleanhtml)) || preg_match("/^(\s|\t)*\{{1}(\s|\t)*$/", $currentuncleanhtml)) {
                //If opening HTML tag AND not a stand-alone tag, or opening JavaScript clams, increase indentation and then apply new level
                $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;

                $indentlevel++;
                $replaceindent = self::indentStr($indentlevel);
            } else{
                //Else, only apply indentation
                $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
            }
        }

        //Return single string seperated by newline
        return implode("\n", $cleanhtml_array); 
    }
}

您可以这样调用:

Html::indent($uncleanhtml);

谢谢,这看起来很有前途。- 有没有特殊的方法来调用这个函数?(我是初学者。)我得到了“解析错误:语法错误,意外的'public'(T_PUBLIC)”。 - mrmut

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接