Substitute for print_r and var_export

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Substitute for print_r and var_export

Post by Shendemiar »

Is there any command or known small nice function that outputs arrays etc. nicely categorized with tabs or emphasized by html.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

You could always write one that parses the values returned from those functions and uses pretty colors.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?php
echo '<pre> '. print_r($array) .'</pre>';
?>
should do it
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

protokol wrote:You could always write one that parses the values returned from those functions and uses pretty colors.
That's what i have in mind, but i assumed it's done and used countless times before, so that's why i asked.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

That's nice, thanks!
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

I made something handy for printing classes with (multi-D)arrays in them.

Code: Select all

$needles=array( "&#123;" , ";" , "(" , "," , ")" , "&#125;" );
$replace=array( "<BLOCKQUOTE>" , "<BR><BR>" , "<BLOCKQUOTE>" , "<BR>", "</BLOCKQUOTE>" , "</BLOCKQUOTE>");
echo str_replace($needles, $replace, var_export($units,1));
Outputs:

Code: Select all

class cUnits

    public $unit_static = NULL
    public $unit = array

        1 => array

            'type_id' => '1'
            'name_number' => '1'
            'name' => 'Moonguard #1'
            'strength' => '1000'
            'x' => '10'
            'y' => '50'


        2 => array

            'type_id' => '1'
            'name_number' => '2'
            'name' => 'Moonguard #2'
            'strength' => '1000'
            'x' => '11'
            'y' => '50'


        4 => array

            'type_id' => '1'
            'name_number' => '3'
            'name' => 'Combined skirmish force of the Wise'
            'strength' => '0'
            'x' => '0'
            'y' => '0'
Post Reply