badah
3/16/2016 - 11:29 PM

PHP - convert any sentence (pt-br) in Title format

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;
}

?>