morpe

jDump

Aug 3rd, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.33 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. --- info ---
  5. jDump usa json_encode per stampare nella console js una variabile da php
  6. tipo $_SERVER, non funziona con $GLOBALS
  7.  
  8. --- install ---
  9. - includere jdump.php all inzio dello script
  10. - per il dump di una variabile/array usare jd(var/array,'label')
  11. - per stampare l'aoutput usare jsOut() da inserire in qualsiasi punto del file, tipo nel footer
  12. */
  13.  
  14.  
  15. class jDump{
  16.     public $script;
  17.     public $input;
  18.     public $alljd;
  19.  
  20.     public function __construct($input='') {
  21.         $this->input = array();
  22.     }
  23.  
  24.     public function o() {
  25.         //var_dump($this->input);
  26.         $js = "<script class='jDumper'>";
  27.         $js .= "jDumpStr='".json_encode($this->input,JSON_HEX_TAG | JSON_UNESCAPED_SLASHES |JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT)."';\n";
  28.         $js .= "jDump = JSON.parse(jDumpStr);\n";
  29.         $js .="console.log('jDump:',jDump);\n";
  30.         $js .= "</script>";
  31.  
  32.         echo $js;
  33.     }
  34.  
  35.     public function in($in,$label=false) {
  36.         //var_dump($this->input);
  37.         if($label){
  38.             $this->input[$label] = $in;
  39.         }
  40.         else
  41.         {
  42.             $this->input[] = $in;
  43.         }
  44.     }
  45. }
  46.  
  47. $jd = new jDump();
  48.  
  49. function jd($in,$label=false){
  50.     global $jd;
  51.     $jd->in($in,$label);
  52. }
  53.  
  54. function jdOut(){
  55.     global $jd;
  56.     $jd->o();
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  ?>
Add Comment
Please, Sign In to add comment