note 84102 added to function.ob-start

From: Date: Fri, 27 Jun 2008 16:35:43 +0000
Subject: note 84102 added to function.ob-start
Groups: php.notes 
Request: Send a blank email to php-notes+get-140882@lists.php.net to get a copy of this message
Here's a nifty function I use daily. Essentially: include a PHP file - but render its output to a variable, rather than to the buffer. It's also set up to load the script with a variable set, and automagically loads globals into the script's namespace, making it an effective templating scheme. It also has error handling, so that you're not flying blind when using output buffering. <?php $GLOBALS['BufferedErrors']=Array(); function errorParse($errno, $errstr, $errfile, $errline, $errcontext) { $errorTypes = Array( E_ERROR => 'Fatal Error', E_WARNING => 'Warning', E_PARSE => 'Parse Error', E_NOTICE => 'Notice', E_CORE_ERROR => 'Fatal Core Error', E_CORE_WARNING => 'Core Warning', E_COMPILE_ERROR => 'Compilation Error', E_COMPILE_WARNING => 'Compilation Warning', E_USER_ERROR => 'Triggered Error', E_USER_WARNING => 'Triggered Warning', E_USER_NOTICE => 'Triggered Notice', E_STRICT => 'Deprecation Notice', E_RECOVERABLE_ERROR => 'Catchable Fatal Error' ); $ret=(object)Array( 'number'=>$errno, 'message'=>$errstr, 'file'=>$errfile, 'line'=>$errline, 'context'=>$errcontext, 'type'=>$errorTypes[$errno] ); $GLOBALS['BufferedErrors'][]=$ret; return false; } function parse($fileToInclude, $argumentsToFile=false) { $bufferedErrorStack = $GLOBALS['BufferedErrors']; set_error_handler('errorParse', error_reporting()); $GLOBALS['BufferedErrors']=Array(); if (!file_exists($fileToInclude)) return ''; if ($argumentsToFile === false) $argumentsToFile = Array(); $argumentsToFile = array_merge($GLOBALS, $argumentsToFile); foreach ($argumentsToFile as $variableName => $variableValue) $$variableName = $variableValue; ob_start(); include($fileToInclude); $ret = ob_get_contents(); ob_end_clean(); restore_error_handler(); $errors = $GLOBALS['BufferedErrors']; $GLOBALS['BufferedErrors'] = $bufferedErrorStack; if (count($errors)>0) { $ret.='<ul class="error">'; foreach ($errors as $error) $ret.= '<li>'. '<b>'.$error->type.'</b>: '. $error->message. '<blockquote>'. '<i>file</i>: '.$error->file.'<br />'. '<i>line</i>: '.$error->line. '</blockquote>'. '</li>'; $ret.='</ul>'; } return $ret; } ---- Server IP: 64.71.164.2 Probable Submitter: 75.147.77.137 ---- Manual Page -- http://www.php.net/manual/en/function.ob-start.php Edit -- https://master.php.net/note/edit/84102 Del: integrated -- https://master.php.net/note/delete/84102/integrated Del: useless -- https://master.php.net/note/delete/84102/useless Del: bad code -- https://master.php.net/note/delete/84102/bad+code Del: spam -- https://master.php.net/note/delete/84102/spam Del: non-english -- https://master.php.net/note/delete/84102/non-english Del: in docs -- https://master.php.net/note/delete/84102/in+docs Del: other reasons-- https://master.php.net/note/delete/84102 Reject -- https://master.php.net/note/reject/84102 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#140882) next »