Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Initial capitalization of strings in PHP

Posted on 2007-03-30
4
Medium Priority
?
547 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
[X]
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
  • 2
4 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 200 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 1600 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 200 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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

670 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