PHP mb_ucfirst Make a String’s First Character Uppercase-Multibyte (UTF-8) Function


/ Published in: PHP
Save to your folder(s)

HP’s ucfirst function is very usefull when you want to change words first letters to uppercase and other letters to lowercase. Currently on PHP does not have a multibyte (UTF-8) version of ucfirst function. So I decided write my own multibyte mb_ucfirst function.

Perhaps the multibyte version of ucfirst function is added later on PHP, so that’s why is better add this function only if it does not already exist.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. if (!function_exists('mb_ucfirst')) {
  4. function mb_ucfirst($str, $encoding = "UTF-8", $lower_str_end = false) {
  5. $first_letter = mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding);
  6. $str_end = "";
  7. if ($lower_str_end) {
  8. $str_end = mb_strtolower(mb_substr($str, 1, mb_strlen($str, $encoding), $encoding), $encoding);
  9. }
  10. else {
  11. $str_end = mb_substr($str, 1, mb_strlen($str, $encoding), $encoding);
  12. }
  13. $str = $first_letter . $str_end;
  14. return $str;
  15. }
  16. }
  17.  
  18. ?>

URL: http://www.if-not-true-then-false.com/2010/php-mb_ucfirst-make-a-strings-first-character-uppercase-multibyte-function/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.