Re: Help needed with mb_convert_encoding()
| From: | Nicolas Grekas | Date: | Wed, 28 May 2014 09:24:27 +0000 |
| Subject: | Re: Help needed with mb_convert_encoding() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-323365@lists.php.net to get a copy of this message | ||
Hi Alain,
I should advertise
patchwork/utf8<https://github.com/nicolas-grekas/Patchwork-UTF8>here
:)
I am trying to use this to validate input that is supposed to be UTF-8 and
> to
> replace any bad characters with something - '?' would do.
>
I'd personally take an other way: ill formed utf-8 sequences do not exist
in non bugged applications. What is much more common is an application that
sends something else than UTF-8. If you don't know what your input charset
encoding is, then HTML5 tells us that CP1252 is a good default fall-back.
Thus, I'd recommend using this snippet (preg_match is the quickest way to
check for utf-8 in PHP):
$input = "...";
if (!preg_match('//u', $input)) {
$input = iconv('CP1252', 'UTF-8', $input);
}
// here, $input is always an utf-8 string
Regards,
Nicolas