PHP 迭代数组查看器和 HTML 查看器






4.40/5 (3投票s)
这个脚本是一个色彩丰富的数组查看工具,适用于包含 HTML 组件的普通数组和迭代数组。
引言
我编写这个脚本是为了快速查看数组和显示其中的 HTML 数据。 许多 IDE 支持调试,但有时需要扫描数组。 PHP 的 Print_r()
方法很棒,但从视觉上看,我认为它对于频繁查看数组来说不太方便,尤其是在递归数组中。 这里有一个解决方案。 这个脚本返回字符串数据,基本上与 print_r()
方法显示的内容相同,但它可以帮助您通过视觉分隔、内部数组制表样式和HTML 源代码模式来查看。
基本方法
<?php
include("arrayViewer.php");
$array=array("first","second","third","fourth");
view_r($array);
?>
对于返回方法,您可以使用 get_r()
方法代替 view_rr()
方法。
echo get_r($array);
输出格式如下
1. 视觉分隔模式
PHP 脚本文件附带一个样式表文件,需要在您的网页中导入该文件才能启用视觉分隔模式,在该模式下,键以红色显示,值以绿色显示。
<LINK REL = Stylesheet TYPE ="text/css" HREF ="arrayViewer.css" >
2. 内部数组制表样式
这个 metThis
方法可以处理内部数组。 内部值比父项制表缩进一个级别。
<?php
include("arrayViewer.php");
$sub_sub_arr=array("sub_sub_item1",sub_sub_item2");
$sub_arr=array("sub_item1",sub_item2",$sub_sub_arr);
$arr=array("item1","item2",$sub_arr);
view_r($arr);
?>
输出将如下所示
HTML 源代码模式
在将 HTML 内容存储在数组项中的情况下,它将允许您仅以默认的网页视图显示 HTML,或者同时显示网页视图和 HTML 源代码视图。
<?php
include("arrayViewer.php");
$chArr=array("< div class='cls1'> </div>");
$array=array("html1"=>"< div class='clas1'> </div>",
"tow"=>"< div class='cls2'> </div>","sub"=>$chArr);
?>
然后,要获取正常的 HTML 视图
view_r($array,"",false);
此功能通常已启用,并且可以选择隐藏 HTML 内容和进一步的属性聚焦。 因此,要仅查看源代码,您只需要调用默认方法
view_r($array);
要获取 HTML 源代码视图,请使用
view_r($array,"",true);
第二个参数保留给递归方法调用中所需的制表空间,用于包含内部数组的数组,并且始终需要设置一个空字符串。
以下是相关的输出(正常模式与 HTML 模式)
聚焦 HTML 属性
它允许您通过将属性名称指定为方法中的第四个属性来突出显示单个 HTML 属性。
view_r($array,"",true,"class");
输出如下
您可以进一步指定已定义 HTML 属性的突出显示选项,如下所示:
view_r($array,"",true,"class","background: green;font-size: 20px;color: #fff;");
然后输出如下
内部架构
function view_r($arr, $tab="", $encode=false, $focus_attr=null, $style=null) {
$return_debug = "<div style='width: 100%;'><pre>";
$return_debug.= "<pre>" . $tab . "[</pre>";
if (is_array($arr)) {
foreach ($arr as $key => $value) {
if (is_array($value)) {
$return_debug.= "<pre>" . $tab .
"<span class='array_key'>$key</span> => </pre>";
$return_debug.=view_r($value, $tab . " ",
$encode, $focus_attr, $style);
} else {
if ($encode) {
$return_debug.= "<pre>" . $tab . "<span class='array_key'>" .
$key . "</span> => <span class='array_value'>" .
$value . "</span></pre>";
$return_debug.= "<pre>" . $tab . "<span class='array_key'>" .
$key . "</span> => <span class='array_value'>" .
encode_html($value, $tab, $focus_attr, $style) . "</span></pre>";
}
else
$return_debug.= "<pre>" . $tab . "<span class='array_key'>" .
$key . "</span> => <span class='array_value'>" .
$value . "</span></pre>";
}
//echo "<pre>".$tab." ]</pre><br />";
}
$return_debug.= "<pre>" . $tab . "]</pre><br />";
}
$return_debug.= "</pre></div>";
return $return_debug;
}
在方法的主要 foreach
循环中,它检测值是否为数组,如果是数组,则递归调用 view_r
方法,如果不是数组,则调用 encode_html()
,后者也可以用于其他目的。 它还接受四个参数用于 HTML 数据:制表符、聚焦属性和属性样式,如 view_r()
方法。
HTML 编码方法
此方法将以彩色样式显示数据的 HTML 源代码,并提供预定义属性的聚焦功能。
echo encode_html(array(“<div class=’box’><h2 class=’heading’>text</h2></div>”,
””,true,”class”,”color: red;”);
encode_html()
方法的内部架构
function encode_html($str, $tab, $focus_attr=null, $style=null) {
$skip = 0;
$current_tag = null;
$html_close = false;
$html_open = false;
$return_str = "";
for ($i = 0; $i < strlen($str); $i++) {
if ($str{$i} == "<" && $skip == 0) {
$return_str.="<mark><</mark>";
$tag_arr = get_tag($str, $i, $focus_attr, $style);
$return_str.=$tag_arr["value"];
$skip = $tag_arr["skip"];
$html_open = true;
} elseif ($str{$i} == ">" && $skip == 0) {
$return_str.="<mark>></mark>\n$tab ";
$html_close = true;
} elseif ($str{$i} == "'" && $skip == 0 && $html_open) {
//$return_str.="<quote>'</quote>";
$quoted = get_quoted_value($str, $i, $focus_attr, $style, $current_tag);
$return_str.=$quoted['value'];
$skip = $quoted['skip'];
} elseif ($str{$i} == " " && $skip == 0 && $html_open) {
//$return_str.="<quote>'</quote>";
$current_tag = get_current_tag($str, $i);
$attr = get_attr($str, $i, $focus_attr, $style);
$return_str.=$attr['value'];
$skip = $attr['skip'];
} elseif ($skip > 0) {
$skip--;
}
else
$return_str.=$str{$i};
}
return $return_str;
}
function get_current_tag($str, $index) {
for ($new = $index + 1; $new < strlen($str); $new++) {
if ($str{$new} != "=" && $str{$new} != "/" &&
$str{$new} != ">" && $str{$new} != "<") {
$cur_tag.=$str{$new};
} else {
break;
}
}
return trim($cur_tag);
}
function get_attr($str, $index, $focus_attr=null, $style=null, $current_tag=null) {
$count = 0;
$return_quoted_arr = null;
if (isset($current_tag))
$quoted_text.="<attr" . check_focus($focus_attr, $current_tag, $style) . "> ";
else
$quoted_text.="<attr" . check_focus($focus_attr, "attr", $style) . "> ";
for ($s = $index + 1; $s < strlen($str); $s++) {
if ($str{$s} != "=" && $str{$s} != ">" && $str{$s} != "/")
$quoted_text.=$str{$s};
else {
$quoted_text.="</attr>";
$count = $s;
break;
}
}
$return_quoted_arr["skip"] = $count - $index - 1;
$return_quoted_arr["value"] = $quoted_text;
return $return_quoted_arr;
}
function get_tag($str, $index, $focus_attr=null, $style=null) {
$count = 0;
$return_quoted_arr = null;
$quoted_text.="<tag" . check_focus($focus_attr, "tag", $style) . ">";
for ($s = $index + 1; $s < strlen($str); $s++) {
if ($str{$s} != " " && $str{$s} != ">")
$quoted_text.=$str{$s};
else {
$quoted_text.="</tag>";
$count = $s;
break;
}
}
$return_quoted_arr["skip"] = $count - $index - 1;
$return_quoted_arr["value"] = $quoted_text;
return $return_quoted_arr;
}
function get_quoted_value($str, $index, $focus_attr=null,
$style=null, $current_tag=null) {
$count = 0;
$return_quoted_arr = null;
if (isset($current_tag))
$quoted_text.="<qoute>'</quote><attrvalue" .
check_focus($focus_attr, $current_tag, $style) .
check_focus($focus_attr, "attrvalue", $style) . ">";
else
$quoted_text.="<qoute>'</quote><attrvalue" .
check_focus($focus_attr, "attrvalue", $style) . ">";
for ($s = $index + 1; $s < strlen($str); $s++) {
if ($str{$s} != " " && $str{$s} != "'")
$quoted_text.=$str{$s};
else {
$quoted_text.="</attrvalue><qoute>'</quote> ";
$count = $s;
break;
}
}
$return_quoted_arr["skip"] = $count - $index;
$return_quoted_arr["value"] = $quoted_text;
return $return_quoted_arr;
}
function check_focus($focus_attr, $attr, $style) {
if ($focus_attr == null) {
return "";
} else {
if ($focus_attr == $attr) {
return " style='color: white; font-size: large;background: black;" .
$style . "' ";
}
else
return "";
}
}