Created
September 19, 2012 18:04
Revisions
-
technosailor revised this gist
Sep 19, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ * @return none * @author Sarfraz */ function logConsole($name, $data = NULL, $jsEval = FALSE) { if (! $name) return false; -
technosailor created this gist
Sep 19, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ <?php /** * Logs messages/variables/data to browser console from within php * * @param $name: message to be shown for optional data/vars * @param $data: variable (scalar/mixed) arrays/objects, etc to be logged * @param $jsEval: whether to apply JS eval() to arrays/objects * * @return none * @author Sarfraz */ public function logConsole($name, $data = NULL, $jsEval = FALSE) { if (! $name) return false; $isevaled = false; $type = ($data || gettype($data)) ? 'Type: ' . gettype($data) : ''; if ($jsEval && (is_array($data) || is_object($data))) { $data = 'eval(' . preg_replace('#[\s\r\n\t\0\x0B]+#', '', json_encode($data)) . ')'; $isevaled = true; } else { $data = json_encode($data); } # sanitalize $data = $data ? $data : ''; $search_array = array("#'#", '#""#', "#''#", "#\n#", "#\r\n#"); $replace_array = array('"', '', '', '\\n', '\\n'); $data = preg_replace($search_array, $replace_array, $data); $data = ltrim(rtrim($data, '"'), '"'); $data = $isevaled ? $data : ($data[0] === "'") ? $data : "'" . $data . "'"; $js = <<<JSCODE \n<script> // fallback - to deal with IE (or browsers that don't have console) if (! window.console) console = {}; console.log = console.log || function(name, data){}; // end of fallback console.log('$name'); console.log('------------------------------------------'); console.log('$type'); console.log($data); console.log('\\n'); </script> JSCODE; echo $js; } # end logConsole