我想自动缩进PHP脚本输出的HTML。
我确实使用HTML Purifier进行内部文本框输入表单验证,并考虑过HTMLTidy。但是,由于HTML净化器依赖于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>";
谢谢!
答案 0 :(得分:2)
这是我的缩进功能,希望它有所帮助。
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);