53 lines
1.4 KiB
PHP
53 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Support\Danal;
|
|
|
|
final class EucKr
|
|
{
|
|
public function toEuc(string $s): string
|
|
{
|
|
if ($s === '') return '';
|
|
$out = @iconv('UTF-8', 'EUC-KR//IGNORE', $s);
|
|
return $out === false ? $s : $out;
|
|
}
|
|
|
|
public function toUtf8(string $s): string
|
|
{
|
|
if ($s === '') return '';
|
|
|
|
// 이미 UTF-8이면 그대로
|
|
if (function_exists('mb_check_encoding') && mb_check_encoding($s, 'UTF-8')) {
|
|
return $s;
|
|
}
|
|
|
|
// EUC-KR -> UTF-8 변환(실패해도 빈문자/대체로 처리)
|
|
$out = @iconv('EUC-KR', 'UTF-8//IGNORE', $s);
|
|
|
|
// iconv가 false면 mb_convert로 한번 더(가능할 때)
|
|
if ($out === false) {
|
|
$out = function_exists('mb_convert_encoding')
|
|
? @mb_convert_encoding($s, 'UTF-8', 'EUC-KR')
|
|
: '';
|
|
}
|
|
|
|
// 그래도 UTF-8이 아니면 마지막으로 invalid byte 제거
|
|
if ($out === false || $out === '') $out = '';
|
|
if (function_exists('mb_check_encoding') && !mb_check_encoding($out, 'UTF-8')) {
|
|
$out = @iconv('UTF-8', 'UTF-8//IGNORE', $out) ?: '';
|
|
}
|
|
|
|
return $out;
|
|
}
|
|
|
|
|
|
public function mapToUtf8(array $arr): array
|
|
{
|
|
foreach ($arr as $k => $v) {
|
|
if (is_string($v) && $v !== '') {
|
|
$arr[$k] = $this->toUtf8($v);
|
|
}
|
|
}
|
|
return $arr;
|
|
}
|
|
}
|