Initial capitalization of strings in PHP

I've got someone's first name in a string, and I'd like to do two things:

1. Make sure that it's capitalized properly: Joe, not JOE, joe, JOe, jOe, etc.
2. Strip any titles (and the titles may be in any permutation of lower/uppercase letters, and may or may not be punctuated: Dr, Mr, Mrs, Ms, and all the variations of those.

Are there functions I can use to do this?

Thanks!
Philly_CAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
for 1) are you aware of the functions ucfirst() and ucwords() in php?
http://www.php.net/manual/en/function.ucfirst.php
http://www.php.net/manual/en/function.ucwords.php

for 2) I will leave that for the regexp experts...
0
glcumminsCommented:
1. To make sure that only the first letter of the word is capitalized, first lower-case the entire word:

$name = strtolower($name);

Then capitalize the first letter:

$name = ucfirst($name);

Or, to do it all at once:

$name = ucfirst(strtolower($name));


2. You should make an array of all the titles that you wish to replace:

$arrTitles = array('Mr', 'Mr.', 'Mrs', 'Mrs.', 'Dr', ...);

Then, use str_replace to remove them:

$name = str_replace($arrTitles, '', $name);

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BrianGEFF719Commented:
@glcummins:
If you're going to do it by an array you should probably have the titles with a dot in them before the titles without the dot... WHY? Case of Mr. Rodgers Well if you replace Mr and then go on to check for Mr., the latter will not be found and you will be stuck with a period before the persons name.
0
glcumminsCommented:
Good catch, Brian. You are exactly right.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.