1

I have strings from a source code and its like;

"REAL MADRİD - BARCELONA"
"VİLLAREAL - IDIONA"
"FENERBAHÇE - BEŞİKTAŞ IŞ"
etc.. etc.. etc..

I want to convert this string totally lowercase and uppercase first character of each word. (like ucwords). But i have a problem with İI and other utf-8 characters when using ucwords function.

I want exactly output for this strings like;

"Real Madrid - Barcelona"
"Villareal - Idıona"
"Fenerbahçe - Beşiktaş Iş"

What can i try? Thanks.

Kadir Çetintaş
  • 207
  • 1
  • 5
  • 13

8 Answers8

1

I found this functiom:

function mb_ucwords($str)
    {
        return mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
    }
Szymon
  • 106
  • 4
0

You should use ucwords with strtolower php method combination

    $s = "REAL MADRID - BARCELONA";
    $s = ucwords(strtolower($s));

    #Real Madrid - Barcelona

Check out this fiddle

For utf8 strings instead of strtolower you can use mb_convert_case method with MB_CASE_TITLE mode like:

    $s = "FENERBAHÇE - BEŞİKTAŞ IŞ";
    $s = mb_convert_case($s, MB_CASE_TITLE, "UTF-8");

Check out this fiddle

please notice that the last one will work for sure also if the first letter of word is special utf8 kind

m.antkowicz
  • 13,268
  • 18
  • 37
0

The function you need is mb_strtolower()

Usage: echo mb_strtolower($str);

For more details, read the Manual.

Manikiran
  • 2,618
  • 1
  • 23
  • 39
0

Need to use strtolower and ucwords

Example :

$string = "REAL MADRİD - BARCELONA";
$lower = strtolower($string); //first need to convert to lower string
$firstupper = ucwords($lower); //then change every word first letter is upper case
Prabu Guna
  • 344
  • 1
  • 3
  • 14
0

try this

$string = "REAL MADRID - BARCELONA";
$ans = ucwords(strtolower ( $string ));
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
Dilip Kale
  • 71
  • 7
0

Try this:

<?php

echo ucwords(strtolower('Dhaka, JAMALPUR, sarishabari'));

Result is:

Dhaka, Jamalpur, Sarishabari
Will
  • 24,082
  • 14
  • 97
  • 108
Parth Chavda
  • 1,819
  • 1
  • 23
  • 30
0

I guess you figured it out, you should use:

mb_strtolower

and

mb_convert_case

Here:

    $games = array(
    "REAL MADRİD - BARCELONA",
    "VİLLAREAL - IDIONA",
    "FENERBAHÇE - BEŞİKTAŞ IŞ",
    "İSTANBULSPOR A.Ş - FENERBAHÇE İŞ"
);

foreach($games as $game) {
    echo mb_convert_case($game, MB_CASE_TITLE, "UTF-8") . '<br>';
}

it will print the result you're looking for:

Real Madrid - Barcelona
Villareal - Idiona
Fenerbahçe - Beşiktaş Iş
İstanbulspor A.ş - Fenerbahçe İş

To capitalize like A.S a solution would be:

foreach($games as $game) {
    $tmp = str_replace(".",". ",$game);
    echo mb_convert_case($tmp, MB_CASE_TITLE, "UTF-8") . '<br>';
}

and you get:

Real Madrid - Barcelona
Villareal - Idiona
Fenerbahçe - Beşiktaş Iş
İstanbulspor A. Ş - Fenerbahçe İş

Of course, you may create your own function, especially if there are other requirements as well.

Florin Sima
  • 1,495
  • 17
  • 13
  • I have problems with I İ characters, Can you check this sample, http://sandbox.onlinephpfunctions.com/code/83db6ce3adfe0780f9b8720820a98cb0cf77da16 – Kadir Çetintaş Dec 31 '15 at 12:06
  • Ok, now I noticed the special 'I' character and I modified accordingly. It remains to address the issue with capitalizing the letter after '.' to get 'A.Ş'. You have to make a custom function for this, I would probably make something simple, like searching for '.' and replace it with '. ' (dot +space). – Florin Sima Dec 31 '15 at 12:40
0

You can try this:

$string = "REAL MADRİD - BARCELONA";
mb_internal_encoding('UTF-8');
$finalString =  mb_convert_case($string, MB_CASE_TITLE);
echo $finalString;

Result:

Real Madrid - Barcelona
devpro
  • 16,184
  • 3
  • 27
  • 38