dreamphp기록과 추억이 머무는 곳
Tip&Tech

배열 키, 값 인코딩 일괄 변경

인코딩이 다른 배열의 키와 값의 인코딩을 변경하는 간단한 방법 하나 소개합니다.
이 역시 오래전 방법입니다만 유용해서 남겨봅니다.

// EUC-KR
$data = array(
'비혼'=>array(
'별명'=>'BiHon',
'홈페이지'=>'http://dreamphp.com',
'언어'=>array('BASIC','C','Perl','PHP','ASP','JSP','Etc.'),
'취미'=>array('음악','영화','기타'),
),
'둘리'=>array(
'식구'=>array('고길동','박정자','고철수','고영희','희동이'),
'친구'=>array('도우너','또치','마이콜'),
'메모'=>'초능력',
),
);

위와 같은 배열이 있을 때 아래의 코드로 간편하게 인코딩을 변경할 수 있습니다.

// UTF-8
eval('$data = '.iconv('EUC-KR','UTF-8',var_export($data,1)).';');

+ eval() … (PHP 4, PHP 5)
- http://php.net/manual/en/function.eval.php
* Caution : The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

+ iconv() … (PHP 4 >= 4.0.5, PHP 5)
- http://php.net/manual/en/function.iconv.php

+ var_export() … (PHP 4 >= 4.2.0, PHP 5)
- http://php.net/manual/kr/function.var-export.php
- http://php.net/manual/en/function.var-export.php
목록

함께 나눈 이야기

댓글

댓글 남기기

이미지와 영상은 본문에 표시됩니다. 삭제한 첨부와 본문 연결은 수정 저장할 때 함께 반영됩니다.

취소

작성자 삭제 후에도 삭제 안내가 남으며 운영자는 원문을 확인할 수 있습니다.