Created
January 23, 2015 11:53
-
-
Save aercolino/f2dc24170834e8d8271e to your computer and use it in GitHub Desktop.
A function for checking the syntax of PHP code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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