Skip to content

Instantly share code, notes, and snippets.

@badah
Created March 16, 2016 23:29
Show Gist options
  • Save badah/230bb14b40fb756446e7 to your computer and use it in GitHub Desktop.
Save badah/230bb14b40fb756446e7 to your computer and use it in GitHub Desktop.
PHP - convert any sentence (pt-br) in Title format
<?php
function title_case($string, $delimiters = array(" ", "-", ".", "'", "O'", "Mc"),
$exceptions = array(
"de", "da", "dos", "das", "do", "I", "e","II", "III", "IV",
"V", "VI", "MBA", "AADM", "em", "in", "and", "or", "of", "EMBA"
)){
$string = mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
foreach ($delimiters as $dlnr => $delimiter) {
$words = explode($delimiter, $string);
$newwords = array();
foreach ($words as $wordnr => $word) {
if (in_array(mb_strtoupper($word, "UTF-8"), $exceptions)) {
// check exceptions list for any words that should be in upper case
$word = mb_strtoupper($word, "UTF-8");
} elseif (in_array(mb_strtolower($word, "UTF-8"), $exceptions)) {
// check exceptions list for any words that should be in upper case
$word = mb_strtolower($word, "UTF-8");
} elseif (!in_array($word, $exceptions)) {
// convert to uppercase (non-utf8 only)
$word = ucfirst($word);
}
array_push($newwords, $word);
}
$string = join($delimiter, $newwords);
}//foreach
return $string;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment