troubleshooting Question

Array Handling in PHP

Avatar of joomla
joomlaFlag for Australia asked on
9 Comments1 Solution298 ViewsLast Modified:
I use this script to ensure surnames are 'propercase' in php/mysql
This script sets the first character as uppercase and the first character after an apostraphe to uppercase as well.
I'd like to add some additional checks.
For example I'd like to check for hyphenated names and double barrelled names
jones-smith becomes Jones-Smith
SMITH JONES becomes Smith Jones
I suspect we need an array to manage the apostraphe, hyphen & space.

if (strstr($surname, '\'')) {
  // Divide the surname into multiple parts, divided by apostrophes
  $surnameDivided = explode('\'', $surname);
  // Capitalize the first letter of each "part" of the surname
  foreach ($surnameDivided as &$part) {
    $part = ucfirst(strtolower($part));
  // Bring the surname together again, with apostrophes
  $surname = implode('\'', $surnameDivided);

// If there is not an apostrophe in the surname,
// capitalize the first letter as you normally would
else {
  $surname = ucfirst(strtolower($surname));
Join our community to see this answer!
Unlock 1 Answer and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros