배열 키, 값 인코딩 일괄 변경
인코딩이 다른 배열의 키와 값의 인코딩을 변경하는 간단한 방법 하나 소개합니다.
이 역시 오래전 방법입니다만 유용해서 남겨봅니다.
위와 같은 배열이 있을 때 아래의 코드로 간편하게 인코딩을 변경할 수 있습니다.
+ 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
이 역시 오래전 방법입니다만 유용해서 남겨봅니다.
// EUC-KR
$data = array(
'비혼'=>array(
'별명'=>'BiHon',
'홈페이지'=>'http://dreamphp.com',
'언어'=>array('BASIC','C','Perl','PHP','ASP','JSP','Etc.'),
'취미'=>array('음악','영화','기타'),
),
'둘리'=>array(
'식구'=>array('고길동','박정자','고철수','고영희','희동이'),
'친구'=>array('도우너','또치','마이콜'),
'메모'=>'초능력',
),
);
$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('$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
함께 나눈 이야기
댓글댓글 남기기