note 90165 modified in function.mb-convert-case by danbrown

From: Date: Thu, 09 Apr 2009 15:04:19 +0000
Subject: note 90165 modified in function.mb-convert-case by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-153022@lists.php.net to get a copy of this message
Building upon Justin's and Alex's work... This function allows you to specify which delimiter(s) to explode on (not just the default space). Now you can correctly capitalize Irish names and hyphenated words (if you want)! <?php function titleCase($string, $delimiters = array(" ", "-", "O'"), $exceptions = array("to", "a", "the", "of", "by", "and", "with", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X")) { /* * 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 */ foreach ($delimiters as $delimiter){ $words = explode($delimiter, $string); $newwords = array(); foreach ($words as $word){ if (in_array(strtoupper($word), $exceptions)){ // check exceptions list for any words that should be in upper case $word = strtoupper($word); } elseif (!in_array($word, $exceptions)){ // convert to uppercase $word = ucfirst($word); } array_push($newwords, $word); } $string = join($delimiter, $newwords); } return $string; } ?> --was-- Building upon Justin's and Alex's work... This function allows you to specify which delimiter(s) to explode on (not just the default space). Now you can correctly capitalize Irish names and hyphenated words (if you want)! <?php function titleCase($string, $delimiters = array(" ", "-", "o'"), $exceptions = array("to", "a", "the", "of", "by", "and", "with", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X")) { /* * 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 */ foreach ($delimiters as $delimiter){ $words = explode(strtoupper($delimiter), $string); $newwords = array(); foreach ($words as $word){ if (in_array(strtoupper($word), $exceptions)){ // check exceptions list for any words that should be in upper case $word = strtoupper($word); } elseif (!in_array($word, $exceptions)){ // convert to uppercase $word = ucfirst($word); } array_push($newwords, $word); } $string = join($delimiter, $newwords); } return $string; } ?> http://php.net/manual/en/function.mb-convert-case.php

« previous php.notes (#153022) next »