0

How to get ucfirst() working with scandinavic characters?

$str = "SÄKYLÄ";
echo ucfirst(strtolower($str)); //prints SÄkylÄ

One possibility is use mb_convert_case() but I'd like to know if this is possible using ucfirst()

$str = "SÄKYLÄ";
echo mb_convert_case($str, MB_CASE_TITLE, "UTF-8"); //prints Säkylä

Which function is faster for string capitalization?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
lingo
  • 1,848
  • 6
  • 28
  • 56
  • According to PHP's `ucfirst` documentation: `in the default "C" locale characters such as umlaut-a (ä) will not be converted.`. So I think you need to run `setlocale()` first: http://php.net/manual/en/function.setlocale.php – Jamesking56 Aug 04 '15 at 13:34

1 Answers1

3

Your problem here is not ucfirst() it's strtolower(). You have to use mb_strtolower(), to get your string in lower case, e.g.

echo ucfirst(mb_strtolower($str));
           //^^^^^^^^^^^^^^ See here

Also you can find a multibyte version of ucfirst() in the comments from the manual:

Simple multi-bytes ucfirst():

<?php

   function my_mb_ucfirst($str) {
       $fc = mb_strtoupper(mb_substr($str, 0, 1));
       return $fc.mb_substr($str, 1);
   }

Code from plemieux from the manual comment

Community
  • 1
  • 1
Rizier123
  • 58,877
  • 16
  • 101
  • 156