Have you ever wanted an easy way to titleize a string? Here it is.

PHP:
  1. /**
  2.      * Create a title out of string
  3.      *
  4.      * @return string
  5.      * @author Justin Palmer
  6.      **/
  7.     public function titleize($string)
  8.     {
  9.         $restricted = array('and', 'but', 'or', 'yet', 'for', 'nor', 'so', 'as',
  10.                             'if', 'once', 'than', 'that', 'till', 'when',
  11.                             'at', 'by', 'down', 'from', 'in', 'into', 'like', 'near', 'of',
  12.                             'off', 'on', 'onto', 'over', 'past', 'to', 'upon', 'with', 'a', 'an', 'the');
  13.         $array = explode(' ', $string);
  14.         for($i = 0; $i <sizeof($array); $i++){
  15.             if($i == 0 || !in_array($array[$i], $restricted) || $i == sizeof($array) - 1){
  16.                 $string .= ucfirst($array[$i]) . ' ';
  17.             }else{
  18.                 $string .= $array[$i] . ' ';
  19.             }
  20.         }
  21.         return trim($string);
  22.     }

Let me know what you think.

Development php string

« »