'php decode 에러'에 해당되는 글 1건

  1. 2019.07.22 php json decode 시 에러 또는 Null뜰때.
2019. 7. 22. 16:43

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

 

'컴퓨터 > PHP' 카테고리의 다른 글

Strict Standards: Only variables should be passed by reference 에러  (0) 2019.07.22
api url 불러오기  (0) 2019.07.22
php 파일이동  (0) 2018.08.28
php 메일 보내기  (0) 2018.08.28
php 웹서버 절대경로 찾기  (0) 2018.08.28
Posted by Tyson