Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Initial capitalization of strings in PHP

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 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