Scripts

print_r()-vervanger

Omdat ik er ziek van werd dat print_r geen handige functies heeft (automatisch pre-tags outputten, in- en uitklappen van arrays of objecten, echte waardes bij bools en nulls) hier maar een eigen knutsel. Het is snel gemaakt en het werkt, maar heel netjes is de code niet. Het is meer om even snel wat te bekijken. Eigenschappen: - Binnen multidimensionale arrays bepaalde arrays in- en uitklappen. Zo ook met functies. - Aangeven hoeveel levels standaard uitgeklapt moeten staan. - Weergave van het aantal childs als een level ingeklapt is. - Highlighting van arrays of objecten d.m.v. een mouseover, - Echte waardes: print_r() geeft bij bools een 1 of 0, deze functie geeft netjes true of false. Zo ook met nulls: print_r() geeft dan niks terug, deze functie null. - Werkt meerdere keren op één pagina. - CSS en JavaScript wordt automatisch één keer neergezet. Plaats je dus meerdere keren een dump van een array op één pagina, dan zal er niet opnieuw CSS en JavaScript geplaatst worden. - Getest en werkend bevonden in Chrome, Firefox 3 en Internet Explorer 7. - Niet valid: style- en script-tags mogen niet in de body staan, waardoor dit niet valid is. Als je zelf de style- en script-tags eruit sloopt en in je head zet, is alles helemaal valid. Parameters: bool print_array ( array $array [, int $levelsOpen=null [, int $identifier=0 [, int $level=0 [, int &$i = 0 ]]]] ) $array is de array die je wilt printen. $levelsOpen is het aantal levels diep die automatisch openstaan. Geef geen waarde mee of een null om alle levels open te zetten. $level, $identifier en $i zijn niet nodig om zelf te gebruiken.

printrvervanger
<?php

function print_array($array, $levelsOpen = null, $identifier = 0, $level = 0, &$i = 0)
{
    if (!is_array($array))
    {
        return false;
    }

    $success = false;

    while (!$success)
    {
        if (!defined('PRINT_ARRAY_' . $identifier) || $level)
        {
            $success = true;
            define('PRINT_ARRAY_' . $identifier, true);
        }
        else
        {
            $identifier ++;
        }
    }

    if (!defined('PRINT_ARRAY_USED'))
    {
        echo '<style type="text/css">.arrayOutputBlock{font-family:"Courier New","Courier",monospace;font-size:12px}.arrayOutputChildBlock{padding-left: 35px}.arrayOutputBlock a{color:red;text-decoration:none}.arrayOutputBlock a:hover{text-decoration:underline}</style><script type="text/javascript">function toggleArrayOutputBlock(i){var top=document.getElementById("arrayOutputChildBlockTop"+i);var block=document.getElementById("arrayOutputChildBlock"+i);if(top.style.display=="none"){top.style.display="inline";block.style.display="none";}else{top.style.display="none";block.style.display="block";}return false;}function toggleHighlightArrayOutputBlock(i){var block=document.getElementById("arrayOutputChildBlock"+i);if(block.style.backgroundColor=="rgb(255, 255, 153)"||block.style.backgroundColor=="rgb(255,255,153)"){block.style.backgroundColor="";}else{block.style.backgroundColor="rgb(255, 255, 153)";}}</script>';
        define('PRINT_ARRAY_USED', true);
    }

    if (!$level)
    {
        echo '<div class="arrayOutputBlock"><a href="#" onclick="return toggleArrayOutputBlock(\'' . $identifier . 'z' . $i . '\');" onmouseover="toggleHighlightArrayOutputBlock(\'' . $identifier . 'z' . $i . '\');" onmouseout="toggleHighlightArrayOutputBlock(\'' . $identifier . 'z' . $i . '\');"><em>Array</em></a> <span id="arrayOutputChildBlockTop' . $identifier . 'z' . $i . '"' . (($levelsOpen !== null && $level + 1 > $levelsOpen) ? '' : ' style="display: none;"') . '>(' . count($array) . ' childs)</span><div class="arrayOutputChildBlock" id="arrayOutputChildBlock' . $identifier . 'z' . $i . '"' . (($levelsOpen !== null && $level + 1 > $levelsOpen) ? ' style="display: none;"' : '') . '>';
        print_array($array, $levelsOpen, $identifier, $level + 1, $i);
        echo '</div></div>';
    }
    else
    {
        foreach ($array as $key => $value)
        {
            if (is_array($value))
            {
                $i ++;
                echo '[' . htmlentities($key, ENT_QUOTES) . '] =&gt; <a href="#" onclick="return toggleArrayOutputBlock(\'' . $identifier . 'z' . $i . '\');" onmouseover="toggleHighlightArrayOutputBlock(\'' . $identifier . 'z' . $i . '\');" onmouseout="toggleHighlightArrayOutputBlock(\'' . $identifier . 'z' . $i . '\');"><em>Array</em></a> <span id="arrayOutputChildBlockTop' . $identifier . 'z' . $i . '"' . (($levelsOpen !== null && $level + 1 > $levelsOpen) ? '' : ' style="display: none;"') . '>(' . count($value) . ' childs)<br /></span><div class="arrayOutputChildBlock" id="arrayOutputChildBlock' . $identifier . 'z' . $i . '"' . (($levelsOpen !== null && $level + 1 > $levelsOpen) ? ' style="display: none;"' : '') . '>';
                print_array($value, $levelsOpen, $identifier, $level + 1, $i);
                echo '</div>';
            }
            elseif (is_object($value))
            {
                $vars = get_object_vars($value);
                $i ++;
                echo '[' . htmlentities($key, ENT_QUOTES) . '] =&gt; <a href="#" onclick="return toggleArrayOutputBlock(\'' . $identifier . 'z' . $i . '\');" onmouseover="toggleHighlightArrayOutputBlock(\'' . $identifier . 'z' . $i . '\');" onmouseout="toggleHighlightArrayOutputBlock(\'' . $identifier . 'z' . $i . '\');"><em>' . htmlentities(get_class($value), ENT_QUOTES) . ' Object</em></a> <span id="arrayOutputChildBlockTop' . $identifier . 'z' . $i . '"' . (($levelsOpen !== null && $level + 1 > $levelsOpen) ? '' : ' style="display: none;"') . '>(' . count($vars) . ' childs)<br /></span><div class="arrayOutputChildBlock" id="arrayOutputChildBlock' . $identifier . 'z' . $i . '"' . (($levelsOpen !== null && $level + 1 > $levelsOpen) ? ' style="display: none;"' : '') . '>';
                print_array($vars, $levelsOpen, $identifier, $level + 1, $i);
                echo '</div>';
            }
            elseif (is_bool($value))
            {
                echo '[' . htmlentities($key, ENT_QUOTES) . '] =&gt; <em>' . (($value) ? 'true' : 'false') . '</em><br />';
            }
            elseif (is_null($value))
            {
                echo '[' . htmlentities($key, ENT_QUOTES) . '] =&gt; <em>null</em><br />';
            }
            else
            {
                echo '[' . htmlentities($key, ENT_QUOTES) . '] =&gt; ' . htmlentities($value, ENT_QUOTES) . '<br />';
            }
        }
    }

    return true;
}

?>

[b]Gebruik:[/b]
<?php

class lol
{
    var $name = 'lol';
}

$array = array(
    true,
    1,
    1.7,
    'lol',
    array_fill(0, 3, 'lol'),
    new lol,
    null
);

print_array($array);

?>

Reacties

0
Nog geen reacties.