ucfirst() function for multibyte character encodings

PhpCharacter Encoding

Php Problem Overview


I've asked about strtolower function. But when using foreign characters it doesn't convert them into uppercase, so I must use:

 mb_strtolower($a,"utf8");

But what can I do, if I want to use ucfirst() function? I haven't found any similar function, where I can set encoding type.

Php Solutions


Solution 1 - Php

There is no mb_ucfirst function, as you've already noticed. You can fake a mb_ucfirst with two mb_substr:

function mb_ucfirst($string, $encoding)
{
	$firstChar = mb_substr($string, 0, 1, $encoding);
	$then = mb_substr($string, 1, null, $encoding);
	return mb_strtoupper($firstChar, $encoding) . $then;
}

Solution 2 - Php

This is more concise solution, although it is rather similar to ucwords function:

$final_string = mb_convert_case($your_string, MB_CASE_TITLE, 'UTF-8');

If you need to capitalize string consist of one word, it is the best solution.

Solution 3 - Php

function mb_ucfirst($string)
{
    return mb_strtoupper(mb_substr($string, 0, 1)).mb_substr($string, 1);
}

Solution 4 - Php

as of 2019-11-18, it seems nobody on stackoverflow got this right, here's how mb_ucfirst() should be implemented in userland:

function mb_ucfirst(string $str, string $encoding = null): string
{
    if ($encoding === null) {
        $encoding = mb_internal_encoding();
    }
    return mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding) . mb_substr($str, 1, null, $encoding);
}

Solution 5 - Php

if (!function_exists('mb_ucfirst'))
{
    function mb_ucfirst($value)
    {
        return mb_strtoupper(mb_substr($value, 0, 1)) . mb_substr($value, 1);
    }
}

Solution 6 - Php

I´m using cp1250 on webpage, and for Ú mb_ucfirst doesn´t work, so little upgrade:

  function mb_ucfirst($string)
{
    $main_encoding = "cp1250"; 
    $inner_encoding = "utf-8";
    $string = iconv($main_encoding, $inner_encoding , $string );
    $strlen = mb_strlen($string);
    $firstChar = mb_substr($string, 0, 1, $inner_encoding);
    $then = mb_substr($string, 1, $strlen - 1, $inner_encoding);
    return $string = iconv($inner_encoding, $main_encoding , mb_strtoupper($firstChar, $inner_encoding) . $then );
}

Solution 7 - Php

/*This worked correctly for me*/
function mb_ucfirst($string, $encoding='UTF-8')
{
    $firstChar = mb_substr($string, 0, 1, $encoding);
    $then = mb_substr($string, 1, mb_strlen($string, $encoding)-1, $encoding);
    return mb_strtoupper($firstChar, $encoding) . $then;
}

Solution 8 - Php

$string = trim(preg_replace('/\s+/', ' ', $string));
$string_ar = explode(' ', mb_strtolower($string,'utf-8'));

foreach($string_ar as $key => $value {
  $string_str .= mb_convert_case(mb_substr(trim($value), 0, 1), MB_CASE_TITLE, 'utf-8')
    . mb_substr(trim($value),1)
    . ' ';
}

$string = trim($string_str);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSimonView Question on Stackoverflow
Solution 1 - PhpzneakView Answer on Stackoverflow
Solution 2 - PhpAlex BelyaevView Answer on Stackoverflow
Solution 3 - PhpKoralek M.View Answer on Stackoverflow
Solution 4 - PhphanshenrikView Answer on Stackoverflow
Solution 5 - PhpgoyoteView Answer on Stackoverflow
Solution 6 - PhpJaroslav ŠtreitView Answer on Stackoverflow
Solution 7 - Phpuser3302248View Answer on Stackoverflow
Solution 8 - PhpParis ZView Answer on Stackoverflow