I've given it some thought and rewritten my function to take full advantage of the CLI -l option (that's lower L). It requires that you enable error reporting via your own php.ini file (which you should edit the function to apply) otherwise the return result is a worthless "Error parsing".
Anyway, I hope this is useful for someone. I'm sure it could use improvement, so use at your own risk. Demo here:
http://kevinpeno.com/projects/php_syntax_check.php
* Check Syntax
* Performs a Syntax check within a php script, without killing the parser (hopefully)
* Do not use this with PHP 5 <= PHP 5.0.4, or rename this function.
*
* @params string PHP to be evaluated
* @return array Parse error info or true for success
**/functionphp_check_syntax($php,$isFile=false)
{# Get the string tokens$tokens=token_get_all('<?php '.trim($php));# Drop our manually entered opening tagarray_shift($tokens);token_fix($tokens);# Check to see how we need to proceed
# prepare the string for parsingif( isset($tokens[0][0] ) &&$tokens[0][0] ===T_OPEN_TAG)$evalStr=$php;
else$evalStr="<?phpn {$php}?>";
if($isFileOR ($tf=tempnam(NULL,'parse-') ANDfile_put_contents($tf,$php) !==FALSE) AND$tf=$php)
{# Prevent outputob_start();system('C:inetpubPHP5.2.6php -c "'.dirname(__FILE__).'/php.ini" -l < '.$php,$ret);$output=ob_get_clean();
if($ret!==0)
{# Parse error to report?if( (bool)preg_match('/Parse error:s*syntax error,(.+?)s+ins+.+?s*lines+(d+)/',$output,$match) )
{
return array('line'=> (int)$match[2],'msg'=>$match[1]
);
}
}
returntrue;
}
returnfalse;
}//fixes related bugs: 29761, 34782 => token_get_all returns <?php NOT as T_OPEN_TAGfunctiontoken_fix( &$tokens) {
if (!is_array($tokens) (count($tokens)<2)) {
return;
}//return of no fixing neededif (is_array($tokens[0]) && (($tokens[0][0]==T_OPEN_TAG) ($tokens[0][0]==T_OPEN_TAG_WITH_ECHO)) ) {
return;
}//continue$p1= (is_array($tokens[0])?$tokens[0][1]:$tokens[0]);$p2= (is_array($tokens[1])?$tokens[1][1]:$tokens[1]);$p3='';
if (($p1.$p2=='') ($p1.$p2=='2) {$p3=is_array($tokens[2])?$tokens[2][1]:$tokens[2];$del= (($p3=='php') ($p3=='='))?3:2;$type= ($p3=='=')?T_OPEN_TAG_WITH_ECHO:$type;
}//rebuild erroneous token$temp= array($type,$p1.$p2.$p3);
if (version_compare(phpversion(),'5.2.2','
if ($del==3)$tokens[2]='';$tokens[0] =$temp;
}
return;
}?>