如何从php文件中获取错误和行号而不运行?

时间:2017-11-02 07:44:31

标签: php wordpress

我试图从index.php文件逐行读取并想显示错误和行号错误。这是我试过的

$code_to_check = "$name = 'soubhagya';echo $name";
$result = eval($code_to_check);

以上编码无效,因为我将$符号放在变量

1 个答案:

答案 0 :(得分:0)

这将解决您的问题

$file = 'file.php';
$searchforError = 'content';

header('Content-Type: text/plain');

$contents = file_get_contents($file);
$pattern = preg_quote($searchforError, '/');
$pattern = "/^.*$pattern.*\$/m";
if(preg_match_all($pattern, $contents, $matches)){
    echo "Found error:\n";
    echo implode("\n", $matches[0]);
    $charpos = strpos($contents, implode("\n", $matches[0]));
    list($before) = str_split($contents, $charpos);
    $line_number = strlen($before) - strlen(str_replace("\n", "", $before)) + 1;
    echo 'on line '.$line_number;
}
else{
   echo "No errors found";
}