note 96179 added to function.ucwords
| From: | jmarois at ca dot ibm dot com | Date: | Fri, 12 Feb 2010 16:51:46 +0000 |
| Subject: | note 96179 added to function.ucwords | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165912@lists.php.net to get a copy of this message | ||
My quick and dirty ucname (Upper Case Name) function.
<?php
//FUNCTION
function ucname($string) {
$string =ucwords(strtolower($string));
foreach (array('-', '\'') as $delimiter) {
if (strpos($string, $delimiter)!==false) {
$string =implode($delimiter, array_map('ucfirst', explode($delimiter, $string)));
}
}
return $string;
}
?>
<?php
//TEST
$names =array(
'JEAN-LUC PICARD',
'MILES O\'BRIEN',
'WILLIAM RIKER',
'geordi la forge',
'bEvErly CRuSHeR'
);
foreach ($names as $name) { print ucname("{$name}\n"); }
//PRINTS:
/*
Jean-Luc Picard
Miles O'Brien
William Riker
Geordi La Forge
Beverly Crusher
*/
?>
You can add more delimiters in the for-each loop array if you want to handle more characters.
----
Server IP: 64.71.164.2
Probable Submitter: 129.42.208.185
----
Manual Page -- http://www.php.net/manual/en/function.ucwords.php
Edit -- https://master.php.net/note/edit/96179
Del: integrated -- https://master.php.net/note/delete/96179/integrated
Del: useless -- https://master.php.net/note/delete/96179/useless
Del: bad code -- https://master.php.net/note/delete/96179/bad+code
Del: spam -- https://master.php.net/note/delete/96179/spam
Del: non-english -- https://master.php.net/note/delete/96179/non-english
Del: in docs -- https://master.php.net/note/delete/96179/in+docs
Del: other reasons-- https://master.php.net/note/delete/96179
Reject -- https://master.php.net/note/reject/96179
Search -- https://master.php.net/manage/user-notes.php