We help IT Professionals succeed at work.

Array Handling in PHP

joomla
joomla asked
on
295 Views
Last Modified: 2013-12-12
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
ie
jones-smith becomes Jones-Smith
and
SMITH JONES becomes Smith Jones
I suspect we need an array to manage the apostraphe, hyphen & space.
thanks
Michael


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));
}
Comment
Watch Question

Try something like this. Not sure I got your idea correctly.

This prints,

Jones Smith | Johnes Smith | Jone'S Smith | Jones-Smith
<?php
$name="jones smith | JOHNES SMITH | jone's smith | jones-smith ";
echo preg_replace("/(\b|-|')([^\b])([^\b]*?)/e","'\\1'.strtoupper('\\2').'\\3'",strtolower($name)).PHP_EOL;
?>

Open in new window

Author

Commented:
Hi shanikawm:
what I'm trying to achieve is to update a set of records which may contain 10,000 records
examples of names might be

o'brien             ---------   O'Brien
barrett-o'reily   ---------   Barrett-O'Reily
smith jones     ----------   Smith Jones
burns               ---------   Burns
CROWTHER   ----------   Crowther

thanks


Hi Joomla,

All your cases are working with my script. Try

$name="o'brien | barrett-o'reily | smith jones | burns | CROWTHER"
or one by one like $name=" barrett-o'reily", $name="barrett-o'reily"

Author

Commented:
Hi,
I may be misunderstanding your script
If I don't know what $name is, will the test work for names like o'connell and brook-jones

regards
M
Hope following code wil be clear for you. I just wrote your code with one line in a different way. It works for apostraphe, hyphen & space.
<?php
$surname="o'connell";
$surname=preg_replace("/(\b|-|')([^\b])([^\b]*?)/e","'\\1'.strtoupper('\\2').'\\3'",strtolower($surname));
echo $surname.PHP_EOL;
?>

// or

<?php
$surname="brook-jones";
$surname=preg_replace("/(\b|-|')([^\b])([^\b]*?)/e","'\\1'.strtoupper('\\2').'\\3'",strtolower($surname));
echo $surname.PHP_EOL;
?>

Open in new window

Most Valuable Expert 2011
Author of the Year 2014

Commented:
Here is the REGEX, annotated.  As you can see from the last example, there is more to getting this right than just capitalizing words.  And there are names from many languages ported into English (like van de Graaf) that do not follow the rules you have set out for capitalization.
<?php // RAY_temp_names.php
error_reporting(E_ALL);
echo "<pre>";

$regex
= "/"             // START REGEX DELIMITER
. "(\b|-|')"      // GROUP 1 - LETTER FOUND AT WORD BOUNDARY, OR DASH, OR APOSTROPHE
. "([^\b])"       // GROUP 2 - LETTER STARTS ON WORD BOUNDARY
. "([^\b]*?)"     // GROUP 3 - LETTER STARTS ON WORD BOUNDARY, ZERO OR MORE CHARS, UNGREEDY
. "/"             // END REGEX DELIMITER
. "e"             // EVALUATE FOR REPLACEMENT
;

$repl
= "'\\1'"
. "."
. "strtoupper('\\2')" // THE UPPERCASE
. "."
. "'\\3'"
;

// TEST NAMES
$names = array
( "o'brien"
, "barrett-o'reily"
, "Barrett-O'Reily"
, "smith jones"
, "burns"
, "CROWTHER"
, "George W. Bush, III"
)
;

// SHOW THE WORK PRODUCTS
foreach ($names as $name)
{
    // $new = preg_replace("/(\b|-|')([^\b])([^\b]*?)/e","'\\1'.strtoupper('\\2').'\\3'",strtolower($name));

    $new = preg_replace($regex, $repl, strtolower($name));
    echo PHP_EOL . "$name $new";
}

Open in new window

Most Valuable Expert 2011
Author of the Year 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
My goodness Ray
excellent script
you don't do anything by half do you?

thanks
M
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Thanks for the points.  It's actually a fairly common question, so I have dealt with it a few times before.  Common rules of grammar get distorted a little bit when we have to deal with proper names.  Best regards, ~Ray

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.