Title Case

/* * Converts to "Title Case". There's lots of prepositions so only the most common 25 are listed here. Add more as needed. * * An example of usage: * $title = "tHe quiCK broWn FoX writes some PhP and goes agaINst apple'S wishes as he JAILbreaks His IphoNE'S ios"; * echo title($title); * Result: * The Quick Brown Fox Writes some PHP and Goes against Apple's Wishes as He Jailbreaks His iPhone's iOS * * @param $title string: A string to be turned into a title. * @return string: The string in "Title Case". * */ function title($title){ $title = strtolower($title); // Lowercase words in titles. $articles = array('a', 'an', 'the', 'some'); $prepositions = array('of', 'in', 'to', 'for', 'with', 'on', 'at', 'from', 'by', 'about', 'as', 'into', 'like', 'through', 'after', 'over', 'between', 'out', 'against', 'during', 'without', 'before', 'under', 'around', 'among'); $conjunctions = array('and', 'but', 'for', 'nor', 'or', 'so', 'yet'); $lcwords = array_merge($articles, $prepositions, $conjunctions); // Title case iterator. $words = explode(' ', $title); $words = explode('-', $title); foreach($words as $i => $word) if($i == 0 or !in_array($word, $lcwords)) $words[$i] = ucwords($word); $title = implode(' ', $words); // Special words to look for. $special = array('HTML', 'iOS', 'iPhone', 'JavaScript', 'MySQL', 'PHP', 'RegEx'); $title = str_ireplace($special, $special, $title); return $title; }

URL to Link

/* * Reads a string and turns URLs into <a></a> HTML tags that you can click on. * * @param $string string: A string possibly containing links. * @return string: The string with HTML tags wrapped around links. */ function linkify($string){ $string = preg_replace('!(((f|ht)tp(s)?://)[-a-zA-Z?-??-?()0-9@:%\!_+.,~#?&;//=]+)!i', '<a href="$1" target="_blank">$1</a>', $string); $string = preg_replace('/\(\<a href\=\"(.*)\)"\ target\=\"\_blank\">(.*)\)\<\/a>/i', '(<a href="$1" target="_blank">$2</a>)', $string); $string = preg_replace('/\<a href\=\"(.*)\."\ target\=\"\_blank\">(.*)\.\<\/a>/i', '<a href="$1" target="_blank">$2</a>.', $string); $string = preg_replace('/\<a href\=\"(.*)\,"\ target\=\"\_blank\">(.*)\,\<\/a>/i', '<a href="$1" target="_blank">$2</a>,', $string); return $string; }

User-Defined Array Organization

/* * Orders an array based on user-defined strings. * * An example of usage: * $array = array('about', 'forum', 'images', 'index', 'news', 'privacy', 'videos'); * $order = array('index', 'news', 'images', 'videos', 'forum'); * $result = sortify($array, $order); * print_r($result); * Result: * Array([0]=>index [1]=>news [2]=>images [3]=>videos [4]=>forum [5]=>privacy [6]=>about) * * An example of sorting a glob: * $nav_list = sortify(glob('views/navigation/*.php'), array('index', 'news', 'images', 'videos', 'forum')); * * @param $array array: The array that will be reordered. * @param $order array: The items to reorder, listed first-to-last. * @return array: The $array reordered according to $order. * */ function sortify($array, $order){ foreach($order as $oldkey => $needle){ foreach($array as $newkey => $haystack){ // In case we're sorting globs. $haystack = pathinfo($haystack, PATHINFO_FILENAME); if($haystack == $needle){ $temp = $array[$oldkey]; $array[$oldkey] = $array[$newkey]; $array[$newkey] = $temp; } } } return $array; }