I want to do:
$str = "<? echo 'abcd'; ?>";
if (is_valid_php($str)){
echo "{$str} is valid";
} else {
echo "{$str} is not valid PHP code";
}
Is there a simple way to do the is_valid_php
check?
All I can find is online php syntax checkers and command-line ways to check syntax, or php_check_syntax
which only works for a file, and I don't want to have to write to a file to check if the string is valid.
I'd rather not use system()
/exec()
or eval()
Related Question - It's old, so I'm hoping there's something new
Other Related Question - all the options (as far as I could tell) either are no longer supported or use command line or have to check files (not strings)
I don't need a full-fledged compiler or anything. I literally only need to know if the string is valid PHP code.
EDIT: By valid php code, I mean that it can be executed, has no compiler/syntax errors, and should contain only PHP code. It could have runtime errors and still be valid, like $y = 33/0;
. And it should contain only PHP... Such as, <div>stuff</div><? echo "str"; ?>
would be invalid, but <? echo "str"; ?>
and echo "$str";
would be valid