Convert the first letter to high box and the accents to low box

Asked

Viewed 42 times

0

I’m bringing the user data from BD Mysql. The names were saved in high box. I am creating a greeting for this user taking only the first name.

list($nome,$sobrenome) = explode(" ",$pe->NomeUsuario);

The result was so:

Welcome back to TULIO

But I want to leave it that way:

Welcome back to Tulio

For that I tried to do the function below:

 function converter($palavra){ //
        $minusculas = array("á", "à", "â", "ã", "ä", "é", "è", "ê", "ë", "í", "ì", "î", "ï", "ó", "ò", "ô", "õ", "ö", "ú", "ù", "û", "ü", "ç");
        $maiusculas = array("Á", "À", "Â", "Ã", "Ä", "É", "È", "Ê", "Ë", "Í", "Ì", "Î", "Ï", "Ó", "Ò", "Ô", "Õ", "Ö", "Ú", "Ù", "Û", "Ü", "Ç");
        $converter = str_replace($maiusculas, $minusculas, $palavra);
        return ucfirst($converter);
      }

 list($nome,$sobrenome) = explode(" ",converter($pe->NomeUsuario));

But the result is:

Thulium

How can I convert TULIO to Tulio?

2 answers

1


If not for accents you could use ucfirst() or ucword(), but in this case it is better to use mb_convert_case()

<?php

    $string = 'TÚLIO';

    $new_string = mb_convert_case($string, MB_CASE_TITLE, 'UTF-8');

    echo $new_string; // Túlio

?>
  • Hello Rafael. I had managed this way and was going to post here $converter = mb_strtolower($palavra,'UTF-8'); return ucfirst($converter);, but your solution was better than mine, because I created a function and solved in only 1 line. Thank you!

0

Then try it this way

function converter($palavra){ //
        $minusculas = array("á", "à", "â", "ã", "ä", "é", "è", "ê", "ë", "í", "ì", "î", "ï", "ó", "ò", "ô", "õ", "ö", "ú", "ù", "û", "ü", "ç");
        $maiusculas = array("Á", "À", "Â", "Ã", "Ä", "É", "È", "Ê", "Ë", "Í", "Ì", "Î", "Ï", "Ó", "Ò", "Ô", "Õ", "Ö", "Ú", "Ù", "Û", "Ü", "Ç");
        $converter = str_replace($maiusculas, $minusculas, $palavra);
        return ucfirst(strtolower($converter));
      }

 list($nome,$sobrenome) = explode(" ",converter($pe->NomeUsuario));

  • Hello Nicolas. Actually it is TULIO and not Ulio, that is, it has accent in the letter . I have tried this way before, but the letter Ú is in high box.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.