加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
php2ast.php 1.16 KB
一键复制 编辑 原始数据 按行查看 历史
nicolasrod 提交于 2020-05-19 15:51 . -am
<?php declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use PhpParser\Error;
use PhpParser\ParserFactory;
function utf8ize($d)
{
if (is_array($d)) {
foreach ($d as $k => $v) {
unset($d[$k]);
$d[utf8ize($k)] = utf8ize($v);
}
} else if (is_object($d)) {
$objVars = get_object_vars($d);
foreach ($objVars as $key => $value) {
$d->$key = utf8ize($value);
}
} else if (is_string($d)) {
return iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($d));
}
return $d;
}
function php2ast()
{
global $argv;
$fname = $argv[1];
if (!is_readable($fname)) {
echo "[-] Error reading file {$fname}.\n";
exit(1);
}
$code = file_get_contents($fname);
if ($code === FALSE) {
echo "[-] Error reading file {$fname}\n";
exit(2);
}
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
try {
$ast = $parser->parse($code);
echo json_encode(utf8ize($ast));
} catch (Error $error) {
echo "[-] Error parsing {$fname}: {$error->getMessage()}\n";
exit(3);
}
}
php2ast();
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化