0
我有一個直接從php documentation site複製的代碼。該代碼使我能夠以可讀格式打印「print_r」功能。代碼如下如何使用javascript更改print_r按鈕名稱
function print_r_tree($data){
// capture the output of print_r
$out = print_r($data, true);
// replace something like '[element] => <newline> (' with <a href="javascript:toggleDisplay('...');">...</a><div id="..." style="display: none;">
$out = preg_replace('/([ \t]*)(\[[^\]]+\][ \t]*\=\>[ \t]*[a-z0-9 \t_]+)\n[ \t]*\(/iUe',
"'\\1<button onClick=\"javascript:toggleDisplay(\''.(\$id = substr(md5(rand().'\\0'), 0, 7)).'\');\">
\\2
</button><div id=\"'.\$id.'\" style=\"display: none;\">'",
$out);
// replace ')' on its own on a new line (surrounded by whitespace is ok) with '</div>
$out = preg_replace('/^\s*\)\s*$/m', '</div>', $out);
// print the javascript function toggleDisplay() and then the transformed output
echo '<script language="Javascript">function toggleDisplay(id) { document.getElementById(id).style.display = (document.getElementById(id).style.display == "block") ? "none" : "block"; }</script>'."\n<pre>$out</pre>";
}
上面的代碼輸出三個按鈕,如[ [0] => Array ], [ [1] => Array ] and [ [2] => Array ]
。 但我希望按以下格式輸出按鈕[Good], [Average], and [Worst]
。我試圖在文本上的按鈕來更改「\ 2」的JavaScript代碼段下面
$out = preg_replace('/([ \t]*)(\[[^\]]+\][ \t]*\=\>[ \t]*[a-z0-9 \t_]+)\n[ \t]*\(/iUe',
"'\\1<button onClick=\"javascript:toggleDisplay(\''.(\$id = substr(md5(rand().'\\0'), 0, 7)).'\');\">
//Writing Javascript code to display buttons instead of "//2"
<script type= \"text/javascript\">
var behaviours = new array('Good', 'Average', 'Worst');
for(var i = 0; i < behaviours.length; i++)
document.write(behaviours[i]);
</script>
</button><div id=\"'.\$id.'\" style=\"display: none;\">'",
$out);
但它給我的正則表達式和標識錯誤。我能做些什麼呢。