Solved

Initial capitalization of strings in PHP

Posted on 2007-03-30
4
538 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 …

776 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