PHP function: mb_ucwords – Uppercase the first character of each word in a string


php function mb_ucwords

Currently on PHP does not have a multibyte (UTF-8) version of ucwords function. I wrote some multibyte mb_ucwords function:

You must call these functions before:

mb_internal_encoding("UTF-8");  // before calling the function
mb_regex_encoding("UTF-8");

Method 1:

if (!function_exists('mb_ucwords') && function_exists('mb_ereg_replace') && function_exists('mb_strtoupper')) {
    function mb_ucwords($string) {
        return mb_ereg_replace("(^| )(\w)","\"\\1\".mb_strtoupper(\"\\2\")",$string,'e');
    }
}

Method 2:

if (!function_exists('mb_ucwords') && function_exists('mb_convert_case')) {
    function mb_ucwords($string) {
        return mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
    }
}

Example:

echo mb_ucwords("ктуальная картина дня");

Result:

Ктуальная Картина Дня

Leave a Reply