How can I use the multibyte string functionality in PHP?
Environment:
Oracle B2C Service, all supported versions
Resolution:
Using PHP string functions like strlen() or substr() may not return the expected result if the string you are working with contains multibyte characters (single characters that take up more than 1 byte of memory). There exists a multibyte extension in PHP specifically created to address this problem through the multibyte counterpart functions like mb_substr() or mb_strlen(). However, the PHP version 5.6 installation included in Oracle B2C Service does not have the multibyte module (mbstring) installed, and it cannot be installed. As of version 25A, however, the mbstring module is enabled when using PHP 8.3.
This means that, when using PHP 5.6 and product versions lower than 25A, functions like mb_substr or mb_strlen are not available. Fortunately, there are slight tweaks that can be done to the non-multibyte functions, used in conjunction with other functions, that let you achieve the same result. Below are two examples of alternatives to the mb_strlen() function (get the length of a multibyte string) and mb_substr (get a substring of a multibyte string).
function my_mb_strlen($input){
$output=0;
if(function_exists('mb_strlen')) {
$output=mb_strlen($input, mb_detect_encoding($input));
} else {
if (preg_match('!!u', $input)) {
$output=strlen( utf8_decode($input));
} else {
$output=strlen($input);
}
}
return $output;
}
function my_mb_substr($string, $offset, $length)
{
$arr = preg_split("//u", $string);
$slice = array_slice($arr, $offset + 1, $length);
return implode("", $slice);
}