UTF-8 BOM导致json_decode的结果为null

问题描述

对于UTF-8无BOM格式的文件,使用file_get_contents()获取其内容之后,使用json_decode()转换为数组时,结果将会为null。

解决方法

将BOM信息给去除

$text = file_get_contents('index.json');
$bom = pack('H*','EFBBBF');
$text = preg_replace("/^$bom/", '', $text);

你可能感兴趣的:(UTF-8 BOM导致json_decode的结果为null)