check syntax before include

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Recently I think about some sort of safe includes which will not fail
if parse error happened. And many different sourses said that this is
imposible without some non standard modules and without calling "perl

I made some investigation and found this way:

function check_syntax($filename)
    // load file
    $code = file_get_contents($file);

    // remove non php blocks
    $code = preg_replace('/(^|\?>).*?(<\?php|$)/i', '', $code);

    // create lambda function
    $f = @create_function('', $code);

    // return function error status
    return !empty($f);

This function will return true if code ok or false if not. Maybe it
will be usefull for somebody...

Dima Vidmich

Site Timeline