note 82811 deleted from function.php-check-syntax by thiago

From: Date: Fri, 12 Dec 2008 11:49:25 +0000
Subject: note 82811 deleted from function.php-check-syntax by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-148072@lists.php.net to get a copy of this message
Note Submitter: kevin at metalaxe dot com ---- Sorry, I'm retarded. I did a little more testing and found big issues. I've fixed them in the following code. This example is now dependant on http://us3.php.net/manual/en/function.token-get-all.php#80335 for a bugfix workaround with token_get_all(); So be sure to include that function in your script when you run this function. <?php /** * Check Eval * Performs a Syntax check without killing the parser (hopefully) * * @params string PHP to be evaluated * @return array Parse error info or true for success **/ function checkEval( $php ) { $evalStr = 'return;'; # Get the string tokens $crack = token_get_all( "<?php\n".$php ); # Drop our manually entered opening tag array_shift( $crack ); token_fix( $crack ); # Check to see how we need to proceed # prepare the string for parsing if( isset( $crack[0][0] ) && ( $crack[0][0] === T_CONSTANT_ENCAPSED_STRING || $crack[0][0] === T_STRING || $crack[0][0] === T_OPEN_TAG ) ){ $evalStr .= '?>'; } # Make sure syntax reporting is on...probably only need E_PARSE...but I'm anal $oldErr = error_reporting( E_ALL | E_STRICT ); # Prevent output ob_start(); eval( $evalStr.$php ); $evaluput = ob_get_clean(); # Reset error reporting error_reporting( $oldErr ); # Only do this if there was output if( $evaluput !== '' ) { # Strip annoying html tags, in case html reporting is enabled in php.ini $evaluput = strip_tags( $evaluput ); # Fancy Regex to pull the error that killed us $pattern = '/Parse error:\s*syntax error,(.+?)\s+in\s+.+?\s*line\s+(\d+)/'; $parse_fail = (bool)preg_match( $pattern, $evaluput, $match ); # Parse error to report? if( $parse_fail === true ) { return array( 'line' => (int)$match[2], 'msg' => $match[1] ); } } return true; } ?>

« previous php.notes (#148072) next »