Jump to content

Recommended Posts

My script forces all names to start with a capital and lower case for the rest.  However, some names have a ' or - in them.

 

For example:

 

Rosie O'Donnell

 

or

 

Carrie-Anne Moss

 

With the script, the "D" in O'Donnell is lower case and the "A" in Anne is lower case.

 

What do I need to do to make them capital?

 

Here is what I am using:

 

<?php echo ucwords(strtolower($row_persons['last_name'])) ?>, <?php echo ucwords(strtolower($row_persons['first_name'])) ?>

You may need a regexp for this. Try the code below. It's sort of a complicated regexp that captures the position of the lowercase letter appearing after an apostrophe or a dash, and I haven't tested it, but give it a whirl:

 

 

$name = ucwords(strtolower($row_persons['last_name'])) . ', ' .  ucwords(strtolower($row_persons['first_name']));

preg_match_all('/[\'\-]([a-z])/', $name, $matches, PREG_OFFSET_CAPTURE);
foreach( $matches as $match_data ) {
   $match_text = $match_data[0][0];
   $match_pos = $match_data[1][1];

   $name = substr_replace($name, strtoupper($name), $match_pos);

}

echo "Fixed name is: {$name}<br />\n";

Close...

 

$name = ucwords(strtolower("O'connor")) . ', ' .  ucwords(strtolower("Matthew"));

preg_match_all('/[\'\-]([a-z])/', $name, $matches, PREG_OFFSET_CAPTURE);
foreach( $matches as $match_data ) {

   $match_text = $match_data[0][0];
   $match_pos = strpos($name, $match_text);

   $name = substr_replace($name, ucwords($match_text), $match_pos, 1);

}

echo "Match 1: $match_text <br />Match Pos: $match_pos<br />Fixed name is: {$name}<br />\n";

You could try this as well

 

$name = ucwords(strtolower("O'connor")) . ', ' .  ucwords(strtolower("Matthew"));

if(strpos($name, "'")) {
$pos = strpos($name, "'") + 1;
}

if(strpos($name, "-")) {
$pos = strpos($name, "-") + 1;
}

$name = substr_replace($name, strtoupper($name[$pos]), $pos, 1);


echo "Fixed name is: {$name}<br />\n";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.