diff --git a/src/IconvTranscoder.php b/src/IconvTranscoder.php index eeccace..b1c4b72 100644 --- a/src/IconvTranscoder.php +++ b/src/IconvTranscoder.php @@ -34,10 +34,13 @@ function ($no, $message) use ($string) { }, E_NOTICE | E_USER_NOTICE ); - - $result = iconv($from, $to ?: $this->defaultEncoding, $string); - restore_error_handler(); - + + try { + $result = iconv($from, $to ?: $this->defaultEncoding, $string); + } finally { + restore_error_handler(); + } + return $result; } } diff --git a/src/MbTranscoder.php b/src/MbTranscoder.php index 9cf7d24..e36d8aa 100644 --- a/src/MbTranscoder.php +++ b/src/MbTranscoder.php @@ -41,7 +41,12 @@ public function transcode($string, $from = null, $to = null) } } - if (!$from || 'auto' === $from) { + if ($to) { + $this->assertSupported($to); + } + + $handleErrors = !$from || 'auto' === $from; + if ($handleErrors) { set_error_handler( function ($no, $warning) use ($string) { throw new UndetectableEncodingException($string, $warning); @@ -50,19 +55,18 @@ function ($no, $warning) use ($string) { ); } - - if ($to) { - $this->assertSupported($to); + try { + $result = mb_convert_encoding( + $string, + $to ?: $this->defaultEncoding, + $from ?: 'auto' + ); + } finally { + if ($handleErrors) { + restore_error_handler(); + } } - - $result = mb_convert_encoding( - $string, - $to ?: $this->defaultEncoding, - $from ?: 'auto' - ); - - restore_error_handler(); - + return $result; }