note 95484 added to function.mb-convert-case

From: Date: Tue, 05 Jan 2010 16:37:17 +0000
Subject: note 95484 added to function.mb-convert-case
Groups: php.notes 
Request: Send a blank email to php-notes+get-164661@lists.php.net to get a copy of this message
A better variant of <b>agash at freemail dot hu</b>'s titleCase function. Works better when you have multiple types of delimiters in a string (eg.: MY STREET I-VIII). function titleCase($string, $exceptions_lower = array("de", "pe", "cu", "la", "si", "nr"), $exceptions_upper = array("I", "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 */ // Remove white spaces from the begining and from the end of text $string = trim($string); // First a simple conversion $string = strtolower($string); $string = ucwords($string); // Find words only preg_match_all("/[a-zA-Z0-9]+/", $string, $match); $words = $match[0]; foreach ($words as $ii=>$vv){ if (in_array(strtolower($vv), $exceptions_lower)){ // Check exceptions list for any words that should be in lower case $new_word = strtolower($vv); } elseif (in_array(strtoupper($vv), $exceptions_upper)){ // Check exceptions list for any words that should be in upper case $new_word = strtoupper($vv); } else { // Convert to uppercase $new_word = ucfirst($vv); } $string = str_replace($vv, $new_word, $string); } return $string; } ---- Server IP: 69.147.83.197 Probable Submitter: 86.105.246.218 ---- Manual Page -- http://www.php.net/manual/en/function.mb-convert-case.php Edit -- https://master.php.net/note/edit/95484 Del: integrated -- https://master.php.net/note/delete/95484/integrated Del: useless -- https://master.php.net/note/delete/95484/useless Del: bad code -- https://master.php.net/note/delete/95484/bad+code Del: spam -- https://master.php.net/note/delete/95484/spam Del: non-english -- https://master.php.net/note/delete/95484/non-english Del: in docs -- https://master.php.net/note/delete/95484/in+docs Del: other reasons-- https://master.php.net/note/delete/95484 Reject -- https://master.php.net/note/reject/95484 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#164661) next »