note 106477 added to function.ucwords
| From: | haijerome at gmail dot com | Date: | Sat, 12 Nov 2011 07:38:31 +0000 |
| Subject: | note 106477 added to function.ucwords | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-184407@lists.php.net to get a copy of this message | ||
Recently i got a request from a client to make the first letter of any word in capital even they are
separated by hyphen.
Eg : donzé pierre-yves => Donzé Pierre-Yves
joye-cagnard frédéric => Joye-Cagnard Frédéric
ucwords("donzé pierre-yves") can make it display
Donzé Pierre-yves but actually i need even the first letter of the second word to be in capital.
So in order to get this i have just written a function below and it works fine. But i hope there
would be a better and easier way to do this if so kindly share.
<?php
$str = "donzé pierre-yves";
echo ucwordsHyphen($str); /*returns Donzé Pierre-Yves*/
function ucwordsHyphen($str){
$converted_str = "";
$str_array = explode(" ",$str);
foreach($str_array as $key=>$value):
if(strpos($value, '-')):
$value = str_replace("-", " ", $value);
$value = ucwords(strtolower($value));
$value = str_replace(" ", "-", $value);
else:
$value = ucwords(strtolower($value));
endif;
$converted_str .= " ".$value;
endforeach;
return $converted_str;
}
?>
Thanks.
And Proud to be a PHP Programmer always :-)
----
Server IP: 69.147.83.197
Probable Submitter: 111.92.52.49
----
Manual Page -- http://www.php.net/manual/en/function.ucwords.php
Edit -- https://master.php.net/note/edit/106477
Del: integrated -- https://master.php.net/note/delete/106477/integrated
Del: useless -- https://master.php.net/note/delete/106477/useless
Del: bad code -- https://master.php.net/note/delete/106477/bad+code
Del: spam -- https://master.php.net/note/delete/106477/spam
Del: non-english -- https://master.php.net/note/delete/106477/non-english
Del: in docs -- https://master.php.net/note/delete/106477/in+docs
Del: other reasons-- https://master.php.net/note/delete/106477
Reject -- https://master.php.net/note/reject/106477
Search -- https://master.php.net/manage/user-notes.php