php
php json decode 시 에러 또는 Null뜰때
관절분리
2021. 9. 16. 15:27
반응형
api로 json 값을 가져와서 계속 decode 하는데 Null값이 뜨고,
안된다고 해서......
찾아보니...
결국
null 또는 스페이스같은게 들어가서 안되는거였다.
$json_string = file_get_contents($api_url);
$data = json_decode( preg_replace('/[\x00-\x1F\x7F]/u', '', $json_string), true );
위처럼 preg_replace 써서, 한번 정리해주니까. 먹힌다...
참고, url
https://stackoverflow.com/questions/2410342/php-json-decode-returns-null-with-valid-json
처음에 아래처럼썼었는데,
$data = json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $json_string), true );
댓글에 누가 머라고 해서, 찾아보니, UTF-8은
$string = preg_replace('/[\x00-\x1F\x7F]/u', '', $string);
이렇게 해야한다..
https://stackoverflow.com/questions/1176904/php-how-to-remove-all-non-printable-characters-in-a-string
출처: https://tyson.tistory.com/169 [TysonWorld]
반응형