php-i18n Digest 12 Oct 2004 14:24:19 -0000 Issue 251
Topics (messages 766 through 767):
Re: iconv error detection
766 by: Moriyoshi Koizumi
767 by: Fredrik Tolf
Administrivia:
To subscribe to the digest, e-mail:
[EMAIL PROTECTED]
To unsubscribe from the digest, e-mail:
[EMAIL PROTECTED]
To post to the list, e-mail:
[EMAIL PROTECTED]
----------------------------------------------------------------------
--- Begin Message ---
Hi,
Just use set_error_handler() to catch errors that occurred within
iconv().
Proof of concept:
<?php
class EncodingConverter
{
var $_last_err;
var $_in_enc;
var $_out_enc;
function EncodingConverter($out_enc, $in_enc) {
$this->_out_enc = $out_enc;
$this->_in_enc = $in_enc;
}
function _handleError($err, $msg) {
$this->_last_err = $msg;
}
function convert($str) {
$this->_last_err = false;
set_error_handler(array(&$this, '_handleError'));
$retval = iconv($_in_enc, $_out_enc, $str);
restore_error_handler();
return $retval;
}
function getLastError() {
return $this->_last_err;
}
}
$conv = &new EncodingConverter("EUC-JP", "Shift_JIS");
$result = $conv->convert("[EMAIL PROTECTED]([EMAIL PROTECTED](Bx01");
if ($conv->getLastError()) {
echo "ERROR: ".$conv->getLastError()."[EMAIL PROTECTED](Bn";
} else {
echo $result."¥n";
}
?>
HTH,
Moriyoshi
On 2004/10/10, at 3:17, Fredrik Tolf wrote:
> I'm writing a lightweight webmail in PHP, Dolda Webmail
> (<http://sf.net/projects/doldawebmail>), and I'm having some trouble
> with the iconv function provided by PHP.
>
> The problem is that when it encounters a multibyte sequence that
> cannot be decoded (like in an erroneously encoded message), it just
> stops there and returns the string up to that point, and I have not
> been able to find out any way of detecting that an error has indeed
> occurred. This is causing me a great deal of trouble...
>
> Does iconv provide a way of detecting if an error has occurred, and if
> so, how?
>
> Thanks for your attention!
>
> Fredrik Tolf
>
> --
> PHP Internationalization Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>
--- End Message ---
--- Begin Message ---
At Mon, 11 Oct 2004 01:22:38 +0900, Moriyoshi Koizumi wrote:
> Hi,
>
> Just use set_error_handler() to catch errors that occurred within
> iconv().
Indeed you are right. I was thinking of that, but I thought it was pointless since I
didn't get an error message printed in the output. It turned out, of course, that I
didn't have error_reporting set as I thought it would be.
Thanks for helping me, it worked out great!
Fredrik Tolf
> On 2004/10/10, at 3:17, Fredrik Tolf wrote:
>
> > I'm writing a lightweight webmail in PHP, Dolda Webmail
> > (<http://sf.net/projects/doldawebmail>), and I'm having some trouble
> > with the iconv function provided by PHP.
> >
> > The problem is that when it encounters a multibyte sequence that
> > cannot be decoded (like in an erroneously encoded message), it just
> > stops there and returns the string up to that point, and I have not
> > been able to find out any way of detecting that an error has indeed
> > occurred. This is causing me a great deal of trouble...
> >
> > Does iconv provide a way of detecting if an error has occurred, and if
> > so, how?
> >
> > Thanks for your attention!
> >
> > Fredrik Tolf
> >
> > --
> > PHP Internationalization Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> >
>
> --
> PHP Internationalization Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
--- End Message ---