Ucfirst () function for multibyte character encodings

I asked about the strtolower function. But when using foreign characters, it does not convert them to uppercase, so I should use:

  mb_strtolower($a,"utf8"); 

But what should I do if I want to use the ucfirst() function? I did not find any similar function where I can set the encoding type.

+56
php character-encoding
Mar 25 '10 at 17:33
source share
13 answers

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

 function mb_ucfirst($string, $encoding) { $strlen = mb_strlen($string, $encoding); $firstChar = mb_substr($string, 0, 1, $encoding); $then = mb_substr($string, 1, $strlen - 1, $encoding); return mb_strtoupper($firstChar, $encoding) . $then; } 
+102
Mar 25 '10 at 17:43
source share

This is a more concise solution, although it is quite similar to the ucwords function:

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

If you need to smooth out a single-word string, this is the best solution.

+62
Aug 22 2018-12-12T00:
source share
 function mb_ucfirst($string) { return mb_strtoupper(mb_substr($string, 0, 1)).mb_strtolower(mb_substr($string, 1)); } 
+16
Jan 04 '13 at 16:52
source share
 if (!function_exists('mb_ucfirst')) { function mb_ucfirst($value) { return mb_strtoupper(mb_substr($value, 0, 1)) . mb_substr($value, 1); } } 
+8
May 12 '15 at 5:49
source share

I use cp1250 on a webpage, and for Ú mb_ucfirst it doesn’t work, so a small update:

  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 ); } 
+2
Apr 02 2018-12-12T00:
source share
 /*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; } 
0
Feb 12 '14 at 15:15
source share

Working version (testing version) for modern PHP:

 <?php function mb_ucfirst($value) { $firstLetter = mb_strtoupper(mb_substr($value, 0, 1, 'UTF-8'), 'UTF-8'); $otherLetters = mb_substr($value, 1, null, 'UTF-8'); return $firstLetter . $otherLetters; } echo mb_ucfirst('żółta źółć'); var_dump('Żółta źółć' === mb_ucfirst('żółta źółć')); 
0
Feb 26 '16 at 10:48
source share

This is the shortest I can figure out ... extract the first word, apply MB_CASE_TITLE and replace it with the original.

function mb_ucfirst($str=''){ $str2=explode(" ",$str); return str_replace($str2[0],mb_convert_case($str2[0], MB_CASE_TITLE, "UTF-8"),$str); }

0
Jun 16 '17 at 1:40
source share
 $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); 
0
May 21 '19 at 11:14
source share
 private static function mb_ucfirst($string) { $first = mb_strtoupper($string[0] . $string[1]); $string[0] = $first[0]; $string[1] = $first[1]; return $string; } 
0
Jun 19 '19 at 16:37
source share
 private static function mb_ucfirst($str) { if (!$str) { return; } $first = mb_strtoupper($str[0] . $str[1]); $str[0] = $first[0]; $str[1] = $first[1]; return $str; } 
0
Jun 19 '19 at 16:46
source share

This is the function:

 function FirstCharString($string){ $aString = explode(' ', $string); $sReturn = ''; foreach ($aString as $k=>$fString) { $sFirst = mb_strtoupper(mb_substr($fString, 0, 1)); $sMore = mb_strtolower(mb_substr($fString, 1)); $sReturn .= $sFirst.$sMore.' '; } return trim($sReturn); } 
-one
Sep 28 '15 at 10:45
source share

Go with this helper:

 $text = "ρασąŽUOlas ΔρασΥκελίζει niekπέΥąΥžuoρ kυνόσ"; $ucfirsted_text = mb_convert_case(mb_substr($text,0,1), MB_CASE_TITLE, "UTF-8").mb_substr($text,1); echo $ucfirsted_text; //will print you "ΡασąŽUOlas ΔρασΥκελίζει niekπέΥąΥžuoρ kυνόσ" 
-one
Oct 25 '16 at 2:57
source share



All Articles