Skip Navigation
Expand
Working with multibyte strings in PHP
Answer ID 10783   |   Last Review Date 03/19/2019

How can I use the multibyte string functionality in PHP?

Environment:

Oracle Service Cloud, 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 installation included in Oracle Service Cloud does not have the multibyte module installed, and it cannot be installed. This means that 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);
}

Available Languages for this Answer:

Notify Me
The page will refresh upon submission. Any pending input will be lost.