Re: Capitalizing names ... needs the final touch...
| From: | Ulf Rompe | Date: | Tue, 08 Oct 2002 10:10:15 +0000 |
| Subject: | Re: Capitalizing names ... needs the final touch... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-119224@lists.php.net to get a copy of this message | ||
"-<[ Rene Brehmer ]>-" <cerberus@mail-telia.dk> writes:
> Firstname Middlename Last-name
>
> But I want the last namepart after the slash (-name) to also start
> with a capital letter.
Just read the comments on ucwords() in the PHP manual, especially the
one from Joerg Krause. He writes:
| None of the examples above recognizes characters normally used as
| delimiters. The following uses a regular expression, which is easy to
| extend, to create an array with the parts of a text. Then it runs the
| ucfirst-function for each part an implode the array:
|
| $text = "What?No delimiters,shit happens here.this solves all problems.";
| preg_match_all("/(\w+[,. ?])+/U", $text, $words);
| foreach($words[0] as $part) $uwords[] = ucfirst($part);
| $text = implode("", $uwords);
| echo $text;
All you have to do is adding the "-" to the character class.
> Isn't there some easy way to do this? If I have to go through a FOR
> routine, it kinda ruins the idea of ucwords() in the first place, so
> I'd rather avoid that if at all possible.
I doubt the loop will be much slower than ucwords().
> Also, I'd like it to correctly be able to also correctly capitalize
> Irish and Scottish names (like O'Connor, and MacDonald).
The O'Connor is no problem, just add "'" to the character class. The
MacDonald is a bit harder to solve. The above function will be of no
help, and just matching for "Mac\w*" won't work since it would also
match other names like "Macke". I think you will have to match these
names against a list of possible words with mid word capitalization.
[x] ulf
--
If you can stay calm, while all around you is chaos... then you
probably haven't completely understood the seriousness of the situation.