note 80370 deleted from function.php-check-syntax by thiago
| From: | thiago@php.net | Date: | Fri, 12 Dec 2008 11:40:31 +0000 |
| Subject: | note 80370 deleted from function.php-check-syntax by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-148066@lists.php.net to get a copy of this message | ||
Note Submitter: oyejorge at googlesemail dot com
----
Nicolas' code is great! Just another check to make sure a } doesn't come before { and
it's much more secure. Still wouldn't trust just anyones code with it though.
<?php
function eval_syntax($code){
$b = 0;
foreach (token_get_all($code) as $token){
if('{' == $token){
++$b;
}elseif('}' == $token){
--$b;
if( $b < 0 ){
return false; // } came before {
}
}
}
// Unbalanced braces would break the eval below
if( $b !== 0){
return false;
}
// Catch potential parse error messages
ob_start();
$code = eval('if(0){' . $code . '}'); // Put $code in a dead code sandbox to
prevent its execution
ob_end_clean();
return false !== $code;
}