What you can do is the following:
Dump your $code to a temporary file.
Execute php using exec( "php -l temporary_file_name" ) ; - notice '-l' option (lint)
Check the output.
Delete temporary file.
If the output is empty then your code is correct.
If the output is non empty then it contains error description.
Here is an example of a code that can do that for you.
Notice that sys_get_temp_dir() is for php >= 5.2.1, and you need to use "" or "/tmp" instead if you have older php
function correct( $code, &$outarr ) {
$tmpfname = tempnam(sys_get_temp_dir(), 'phplint.' ) ;
file_put_contents( $tmpfname, "<?php " . $code . " ?>" ) ;
$dummy = exec( "php -l " . $tmpfname . " 2>&1", $outarr , $rv ) ;
unlink( $tmpfname ) ;
return ( $rv == 0 ) ;
}
And example of usage:
$code = "wrong=;" ;
if ( !correct( $code, $outarr )) {
echo "Code is not correct \n" ;
print_r( $outarr ) ;
}
You can, of course, play with outarr, to display better message or use even other methods to catch output like ob_get_contents()
I hope it will help.