Solved

Initial capitalization of strings in PHP

Posted on 2007-03-30
4
532 Views
Last Modified: 2008-02-01
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!
0
Comment
Question by:Philly_C
  • 2
4 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 50 total points
ID: 18824804
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
 
LVL 24

Accepted Solution

by:
glcummins earned 400 total points
ID: 18824837
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
 
LVL 19

Assisted Solution

by:BrianGEFF719
BrianGEFF719 earned 50 total points
ID: 18824910
@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
 
LVL 24

Expert Comment

by:glcummins
ID: 18825322
Good catch, Brian. You are exactly right.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now