1

I am using this code to keep first letter uppercase and rest lowercase but it doesn't support international latin letters. What should i add to make it support?

str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($name))))

EDIT: ucfirst doesn't do it because i need each word's first letter uppercase

Example: HELLO BABY HOW ARE YOU

I need it like: Hello Baby How Are You

neowinston
  • 7,584
  • 10
  • 52
  • 83
Utku Dalmaz
  • 9,780
  • 28
  • 90
  • 130

5 Answers5

3

This works for international chars also:

mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
Marek
  • 7,337
  • 1
  • 22
  • 33
2

You can use ucwords with strtolower

$string ="HELLO BABY HOW ARE YOU";
echo ucwords(strtolower($string));

Outputs :

Hello Baby How Are You.
fortune
  • 3,361
  • 1
  • 20
  • 30
1

You can first use strtolower to make the string lowercase before passing to ucwords.

ucwords does work with international characters, providing the correct locale is set.

See documentation on setlocale: http://php.net/manual/en/function.setlocale.php

You will need to set LC_CTYPE which is used for character classification and conversion.

Example for German characters:

setlocale(LC_CTYPE, 'de');
$a = ucwords(strtolower($b));
Mitch Satchwell
  • 4,770
  • 2
  • 24
  • 31
0

Here's the code:

function capitalizeFirstLetter($sentence){
    $result = "";
    $words = explode(" ", $sentence);
    for($i=0; $i<sizeof($words); $i++){
        $words[$i] = strtolower($words[$i]);
        $words[$i] = ucfirst($words[$i]);
        $result .= $words[$i];
        if($i != (sizeof($words) - 1)){
            $result .= " ";
        }
    }
    return $result;
}

$test = "HELLO BABY HOW ARE YOU";
echo capitalizeFirstLetter($test);

I couldn't find a way to avoid a loop.

Wissam El-Kik
  • 2,469
  • 1
  • 17
  • 21
-1

Have you looked at using ucfirst? Here is the documentation.

Someone posted code for a sentence_case version ucfirst in the comments section.

Matt Shank
  • 158
  • 1
  • 1
  • 12