Skip to content

Instantly share code, notes, and snippets.

@aercolino
Created January 23, 2015 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aercolino/f2dc24170834e8d8271e to your computer and use it in GitHub Desktop.
Save aercolino/f2dc24170834e8d8271e to your computer and use it in GitHub Desktop.
A function for checking the syntax of PHP code.
/**
* Check the syntax of a code snippet.
*
* @param $code
*
* @return mixed|null|string
*/
function php_lint( $code )
{
$result = null;
if ( ! function_exists('shell_exec') ) {
return $result;
}
$temp = tmpfile();
$meta = stream_get_meta_data($temp);
$filename = $meta['uri'];
fwrite($temp, "<?php $code");
$result = shell_exec("php -n -l $filename"); // -n = no ini, -l = only lint
fclose($temp);
$result = trim($result);
$result = str_replace("in $filename on", 'on', $result);
$result = str_replace("\nErrors parsing $filename", '', $result);
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment