From 6cf51a738c7e1396543c03227e0b0e537060e498 Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Sat, 6 Jun 2009 19:12:49 +0200 Subject: [PATCH] [svn r21274] FS#306 - The multi-byte string library: Adding a function that calculates correctly length of binary data (as number of bytes) regardless the PHP configuration (See the ini-setting mbstring.func_overload). This function may be upgraded for PHP6 in the future. --- .../lib/multibyte_string_functions.lib.php | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/main/inc/lib/multibyte_string_functions.lib.php b/main/inc/lib/multibyte_string_functions.lib.php index 3de8368b9a..48397494f3 100644 --- a/main/inc/lib/multibyte_string_functions.lib.php +++ b/main/inc/lib/multibyte_string_functions.lib.php @@ -39,6 +39,32 @@ * be activated too. */ +/** + * ---------------------------------------------------------------------------- + * A safe way to calculate binary lenght of a string (as number of bytes) + * ---------------------------------------------------------------------------- + */ + +/** + * Calculates binary lenght of a string, as number of bytes, regardless the php-setting mbstring.func_overload. + * This function should work for all multi-byte related changes of PHP5 configuration. + * @param string $string The input string. + * @return int Returns the length of the input string (or binary data) as number of bytes. + */ +function api_byte_count($string) { + static $use_mb_strlen; + if (!isset($use_mb_strlen)) { + $use_mb_strlen = function_exists('mb_strlen') && ((int) ini_get('mbstring.func_overload') & 2); + } + if ($use_mb_strlen) { + return mb_strlen($string, '8bit'); + } + return strlen($string); + + // For PHP6 this function probably will contain: + //return strlen((binary)$string); +} + /** * ---------------------------------------------------------------------------- * Multibyte string conversion functions