phpunit单元测试时,phpstrorm打印输出json格式化缩进方法 [ 2.0 版本 ]
public function printJson($array)
{
var_export($this->jsonFormat(json_encode($array, JSON_UNESCAPED_UNICODE)));
}
function jsonFormat($data, $indent = null)
{
// 缩进处理
$ret = '';
$pos = 0;
$length = strlen($data);
$indent = isset($indent) ? $indent : ' ';
$newline = "\n";
$prevChar = '';
$outofquotes = true;
for ($i = 0; $i <= $length; $i++) {
$char = substr($data, $i, 1);
if ($char == '"' && $prevChar != '\\') {
$outofquotes = !$outofquotes;
} elseif (($char == '}' || $char == ']') && $outofquotes) {
$ret .= $newline;
$pos--;
for ($j = 0; $j < $pos; $j++) {
$ret .= $indent;
}
}
$ret .= $char;
if (($char == ',' || $char == '{' || $char == '[') && $outofquotes) {
$ret .= $newline;
if ($char == '{' || $char == '[') {
$pos++;
}
for ($j = 0; $j < $pos; $j++) {
$ret .= $indent;
}
}
$prevChar = $char;
}
return $ret;
}
cleargo 北京
注册时间:2016-12-05
最后登录:2024-11-11
在线时长:53小时29分
最后登录:2024-11-11
在线时长:53小时29分
- 粉丝33
- 金钱21705
- 威望30
- 积分22535
共 0 条评论