print_rn()
Tatane |
mercredi 20 juillet 2005 à 08:22 |
PHP |
#6 |
RSS
Voici une petite fonction dont je ne peux plus me passer pour débuguer, une petite amélioration de print_r().
La version de base
Voici la première version réalisée il y'a quelques temps déj� ...
function print_rn($data) { echo '<fieldset style="border: 1px solid orange; padding: 5px;color: #333; background-color: #fff;">'; echo '<legend style="border:1px solid orange;padding: 1px;background-color:#eee;color:orange;">'.basename($_SERVER['SCRIPT_FILENAME']).'</legend>'; echo '<pre wrap>'.htmlentities(print_r($data,1)).'</pre>'; echo '</fieldset><br />'; }
La version améliorée
Celle ci n'est pas de moi, je l'ai trouvé sur le net je ne sais plus où (dsl pour l'auteur) ...
Elle intègre la coloration syntaxique et le typage des variables ( un peu � la var_dump() ).
function print_rn($data) { ob_start(); var_dump($data); $c = ob_get_contents(); ob_end_clean(); $c = preg_replace("/\r\n|\r/", "\n", $c); $c = str_replace("]=>\n", '] = ', $c); $c = preg_replace('/= {2,}/', '= ', $c); $c = preg_replace("/\[\"(.*?)\"\] = /i", "[$1] = ", $c); $c = preg_replace('/ /', " ", $c); $c = preg_replace("/\"\"(.*?)\"/i", "\"$1\"", $c); $c = htmlspecialchars($c, ENT_NOQUOTES); // Expand numbers (ie. int(2) 10 => int(1) 2 10, float(6) 128.64 => float(1) 6 128.64 etc.) $c = preg_replace("/(int|float)\(([0-9\.]+)\)/ie", "'$1('.strlen('$2').') <span class=\"number\">$2</span>'", $c); // Syntax Highlighting of Strings. This seems cryptic, but it will also allow non-terminated strings to get parsed. $c = preg_replace("/(\[[\w ]+\] = string\([0-9]+\) )\"(.*?)/sim", "$1<span class=\"string\">\"", $c); $c = preg_replace("/(\"\n{1,})( {0,}\})/sim", "$1</span>$2", $c); $c = preg_replace("/(\"\n{1,})( {0,}\[)/sim", "$1</span>$2", $c); $c = preg_replace("/(string\([0-9]+\) )\"(.*?)\"\n/sim", "$1<span class=\"string\">\"$2\"</span>\n", $c); $regex = array( // Numberrs 'numbers' => array('/(^|] = )(array|float|int|string|resource|object\(.*\)|\&object\(.*\))\(([0-9\.]+)\)/i', '$1$2(<span class="number">$3</span>)'), // Keywords 'null' => array('/(^|] = )(null)/i', '$1<span class="keyword">$2</span>'), 'bool' => array('/(bool)\((true|false)\)/i', '$1(<span class="keyword">$2</span>)'), // Types 'types' => array('/(of type )\((.*)\)/i', '$1(<span class="type">$2</span>)'), // Objects 'object' => array('/(object|\&object)\(([\w]+)\)/i', '$1(<span class="object">$2</span>)'), // Function 'function' => array('/(^|] = )(array|string|int|float|bool|resource|object|\&object)\(/i', '$1<span class="function">$2</span>('), ); foreach ($regex as $x) { $c = preg_replace($x[0], $x[1], $c); } $style = ' /* outside div - it will float and match the screen */ .dumpr { margin: 2px; padding: 2px; background-color: #fbfbfb; float: left; clear: both; } /* font size and family */ .dumpr pre { color: #000000; font-size: 9pt; font-family: "Courier New",Courier,Monaco,monospace; margin: 0px; padding-top: 5px; padding-bottom: 7px; padding-left: 9px; padding-right: 9px; } /* inside div */ .dumpr div { background-color: #fcfcfc; border: 1px solid #d9d9d9; float: left; clear: both; } /* syntax highlighting */ .dumpr span.string {color: #c40000;} .dumpr span.number {color: #ff0000;} .dumpr span.keyword {color: #007200;} .dumpr span.function {color: #0000c4;} .dumpr span.object {color: #ac00ac;} .dumpr span.type {color: #0072c4;} .legenddumpr { background-color: #fcfcfc; border: 1px solid #d9d9d9; padding: 2px; } '; $style = preg_replace("/ {2,}/", "", $style); $style = preg_replace("/\t|\r\n|\r|\n/", "", $style); $style = preg_replace("/\/\*.*?\*\//i", '', $style); $style = str_replace('}', '} ', $style); $style = str_replace(' {', '{', $style); $style = trim($style); $c = trim($c); $c = preg_replace("/\n<\/span>/", "</span>\n", $c); echo "<style type=\"text/css\">".$style."</style>\n"; echo '<fieldset class="dumpr">'; echo '<legend class="legenddumpr">'.basename($_SERVER['SCRIPT_FILENAME']).'</legend>'; //echo '<div>'; echo '<pre>'.$c.'</pre>'; //echo '</div>'; echo '</fieldset>'; echo "<div style=\"clear:both;\"> </div>"; }
Ce qui nous donne ...
$A_datas = array('idParent' => -1, 'icone' => array('url', 'target', array('width'=>12, 'height'=>15)), 'intitule' => array('label'=>'Root', 'lien'=> array('url', 'target', array('width'=>12, 'height'=>15))), 'type' => 'ROOT', 'cle' => 12 ); print_rn($A_datas);
Dans le premier cas :
Array ( [1] => Array ( [idParent] => -1 [icone] => Array ( [0] => url [1] => target [2] => Array ( [width] => 12 [height] => 15 ) ) [intitule] => Array ( [label] => Root [lien] => Array ( [0] => url [1] => target [2] => Array ( [width] => 12 [height] => 15 ) ) ) [type] => ROOT [cle] => 12 ) )
et dans le deuxième cas :
array(1) { [1] = array(5) { [idParent] = int(-1) [icone] = array(3) { [0] = string(3) "url" [1] = string(6) "target" [2] = array(2) { [width] = int(2) 12 [height] = int(2) 15 } } [intitule] = array(2) { [label] = string(4) "Root" [lien] = array(3) { [0] = string(3) "url" [1] = string(6) "target" [2] = array(2) { [width] = int(2) 12 [height] = int(2) 15 } } } [type] = string(4) "ROOT" [cle] = int(2) 12 } }
Pratique non ?!
MAJ 11-01-2007
je vous conseille fortement la petite évolution de Nico, il vient de rajouter LE truc qui manquait à cette fonction : http://www.blog.cactuscrew.com/77-print_rn

Commentaires
1. Le mercredi 20 juillet 2005 à 12:13, par jean
2. Le mercredi 20 juillet 2005 à 12:52, par NiKo
3. Le mercredi 20 juillet 2005 à 12:56, par Tatane
4. Le jeudi 21 juillet 2005 à 20:52, par jean
5. Le mercredi 28 septembre 2005 à 16:14, par PiTiLeZarD
6. Le mercredi 28 septembre 2005 à 16:18, par Tatane
7. Le mardi 13 décembre 2005 à 22:46, par nico
8. Le mardi 16 mai 2006 à 16:44, par Machaon
9. Le mardi 16 mai 2006 à 16:50, par Tatane
10. Le jeudi 11 janvier 2007 à 09:40, par nico
Ajouter un commentaire
Les commentaires pour ce billet sont fermés.