This function returns true incorrectly sometimes (at least when internal encoding is UTF-8 and encoding being checked for is Windows-1251, and PHP version 4.4.4 on FreeBSD 6.2 STABLE). This bug was not answered in bug reporting yet, so here is a workaround:
Instead of checking for this:
<?php
mb_check_encoding($var, $destination_encoding)
?>
check for this:
<?php
mb_check_encoding($var, $destination_encoding) && mb_substr_count($var, '?', $source_encoding) == mb_substr_count(mb_convert_encoding($var, $destination_encoding, $source_encoding), '?', $destination_encoding)
?>
The idea is to count question mark characters ('?') in original value and converted value, because mb_convert_encoding function uses question marks to substitute characters that are not valid in destination encoding.
mb_check_encoding
(PHP 4 >= 4.4.3, PHP 5 >= 5.1.3)
mb_check_encoding — Check if the string is valid for the specified encoding
Description
bool mb_check_encoding ( [string $var [, string $encoding]] )mb_check_encoding() checks if the specified byte stream is valid for the specified encoding. It is useful to prevent so-called "Invalid Encoding Attack".
Parameters
- var
byte stream to check. If it is omitted, this function checks all the input from the beginning of the request.
- encoding
Expected encoding.
Return Values
Returns TRUE on success or FALSE on failure.
mb_check_encoding
Olexa Riznyk
12-Jul-2007 11:53
12-Jul-2007 11:53