If uppercase names are converted to the right thing, O'Hara, MacDonald, Van der Sloat, etc. are processed.

I am given a list of names in uppercase. In order to welcome in the email, I would like them to be appropriate.

Easy enough to do using PHP ucwords . But I feel like I need a regular expression function to handle common exceptions, such as:

O'Hara, MacDonald, Van der Sloat, etc.

It's not so much that I need help creating a regex expression to handle the three examples above (that would be good), since I don't know what all the general exceptions might be.

Surely someone has encountered this problem before, any pointers to published solutions or something that you could share?

+5
source share
3 answers

Using regular expressions in the short list provided can be simple, but if you have to process hundreds or thousands of records, it is very difficult to make bulletproof.

I would rather use something that cannot affect someone else. How do you know if MACDONALD prefers MacDonald?

You are fixing the error of another user. If the source cannot be fixed, you can use something like this:

<?php

$provided_names = array(
  "SMITH",
  "O'HARA",
  "MCDONALD",
  "JONES",
  "VAN DER SLOOT",
  "MACDONALD"
);

$corrected_names = array(
  "O'HARA"        => "O'Hara",
  "MCDONALD"      => "McDonald",
  "VAN DER SLOOT" => "van der Sloot"
);

$email_text = array();

foreach ($provided_names as $provided_name)
{
  $provided_name = !array_key_exists($provided_name, $corrected_names) 
    ? ucwords(strtolower($provided_name)) 
    : $corrected_names[$provided_name];
  $email_text[]  = "{$provided_name}, your message text.";
}

print_r($email_text);

/* output:
Array
(
  [0] => Smith, your message text.
  [1] => O'Hara, your message text.
  [2] => McDonald, your message text.
  [3] => Jones, your message text.
  [4] => van der Sloot, your message text.
  [5] => Macdonald, your message text.
)
*/
?>

Hope this will be helpful.

+2
source

, . , . 100% , .

:

mary-jane => Mary-Jane

o'brien => O'Brien

Joël VON WINTEREGG => Joël von Winteregg

jose de la acosta => Jose de la Acosta

, , . , .

function name_title_case($str)
{
  // name parts that should be lowercase in most cases
  $ok_to_be_lower = array('av','af','da','dal','de','del','der','di','la','le','van','der','den','vel','von');
  // name parts that should be lower even if at the beginning of a name
  $always_lower   = array('van', 'der');

  // Create an array from the parts of the string passed in
  $parts = explode(" ", mb_strtolower($str));

  foreach ($parts as $part)
  {
    (in_array($part, $ok_to_be_lower)) ? $rules[$part] = 'nocaps' : $rules[$part] = 'caps';
  }

  // Determine the first part in the string
  reset($rules);
  $first_part = key($rules);

  // Loop through and cap-or-dont-cap
  foreach ($rules as $part => $rule)
  {
    if ($rule == 'caps')
    {
      // ucfirst() words and also takes into account apostrophes and hyphens like this:
      // O'brien -> O'Brien || mary-kaye -> Mary-Kaye
      $part = str_replace('- ','-',ucwords(str_replace('-','- ', $part)));
      $c13n[] = str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', $part)));
    }
    else if ($part == $first_part && !in_array($part, $always_lower))
    {
      // If the first part of the string is ok_to_be_lower, cap it anyway
      $c13n[] = ucfirst($part);
    }
    else
    {
      $c13n[] = $part;
    }
  }

  $titleized = implode(' ', $c13n);

  return trim($titleized);
}
+2

: https://github.com/tamtamchik/namecase Composer.

, , :

<?php

require_once 'vendor/autoload.php'; // Composer autoload

$arr = ["O'HARA", "MCDONALD", "VAN DER SLOOT"];

foreach ($arr as $name) {
    echo $name . ' => ' . str_name_case($name) . PHP_EOL;
}

The call function str_name_casethat comes with lib on any line of the name will be converted to the corresponding register. For your examples, the output will be as follows:

O'HARA =>
O'HARA MCDONALD => MacDonald
VAN DER SLOOT => Van der Slough

+2
source

All Articles