src/Bandolier/Type/Strings.php
public static function titleCase(
        $value,
        $delimiters = [" ", "-", ".", "'", "O'", "Mc"],
        $exceptions = ["and", "to", "of", "das", "dos", "I", "II", "III", "IV", "V", "VI"]
    ) {
        // if value passed as empty or is not a string then return false
        if (!$value) {
            return false;
        }

        /** @var string $cache Cache Value */
        $cache = self::cacheString($value, $delimiters, $exceptions);

        // check for cache
        if (isset(static::$titleCaseCache[$cache])) {
            return static::$titleCaseCache[$cache];
        }

        /*
         * Exceptions in lower case are words you don't want converted
         * Exceptions all in upper case are any words you don't want converted to title case
         *   but should be converted to upper case, e.g.:
         *   king henry viii or king henry Viii should be King Henry VIII
         */
        $value = mb_convert_case($value, MB_CASE_TITLE, "UTF-8");
        foreach ($delimiters as $delimiter) {
            $words = explode($delimiter, $value);
            $newWords = [];
            foreach ($words as $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);
            }
            $value = join($delimiter, $newWords);
        }
        return static::$formatForTitleCache[$cache] = $value;
    }
public static function titleCase(
        $value,
        $delimiters = [" ", "-", ".", "'", "O'", "Mc"],
        $exceptions = ["and", "to", "of", "das", "dos", "I", "II", "III", "IV", "V", "VI"]
    ) {
        // if value passed as empty or is not a string then return false
        if (!$value) {
            return false;
        }

        /** @var string $cache Cache Value */
        $cache = self::cacheString($value, $delimiters, $exceptions);

        // check for cache
        if (isset(static::$titleCaseCache[$cache])) {
            return static::$titleCaseCache[$cache];
        }

        /*
         * Exceptions in lower case are words you don't want converted
         * Exceptions all in upper case are any words you don't want converted to title case
         *   but should be converted to upper case, e.g.:
         *   king henry viii or king henry Viii should be King Henry VIII
         */
        $value = mb_convert_case($value, MB_CASE_TITLE, "UTF-8");
        foreach ($delimiters as $delimiter) {
            $words = explode($delimiter, $value);
            $newWords = [];
            foreach ($words as $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);
            }
            $value = join($delimiter, $newWords);
        }
        return static::$formatForTitleCache[$cache] = $value;
    }
public static function titleCase ( $ value , $ delimiters = [ " " , "-" , "." , "'" , "O'" , "Mc" ] , $ exceptions = [ "and" , "to" , "of" , "das" , "dos" , "I" , "II" , "III" , "IV" , "V" , "VI" ] ) { // if value passed as empty or is not a string then return false if ( ! $ value ) { return false ; } /** @var string $cache Cache Value */ $ cache = self :: cacheString ( $ value , $ delimiters , $ exceptions ) ; // check for cache if ( isset ( static :: $ titleCaseCache [ $ cache ] ) ) { return static :: $ titleCaseCache [ $ cache ] ; } /*
         * Exceptions in lower case are words you don't want converted
         * Exceptions all in upper case are any words you don't want converted to title case
         *   but should be converted to upper case, e.g.:
         *   king henry viii or king henry Viii should be King Henry VIII
         */ $ value = mb_convert_case ( $ value , MB_CASE_TITLE , "UTF-8" ) ; foreach ( $ delimiters as $ delimiter ) { $ words = explode ( $ delimiter , $ value ) ; $ newWords = [ ] ; foreach ( $ words as $ 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 ) ; } $ value = join ( $ delimiter , $ newWords ) ; } return static :: $ formatForTitleCache [ $ cache ] = $ value ; }
Convert a string to Title Case

@param string $value      String to convert
@param array  $delimiters Delimiters to break into apart words
@param array  $exceptions strings to skip when capitalizing
@return string|bool
Convert a string to Title Case