You are here

public static function BiblioContributorUtility::parseContributorName in Bibliography Module 7.3

Get each part of a contributor's name separately.

Parameters

$full_name: Full name of contributor.

Return value

Array of parsed name, ready for creating a contributor.

1 call to BiblioContributorUtility::parseContributorName()
BiblioContributorUtility::getBiblioContributorsFromNames in includes/BiblioContributorUtility.inc
Get saved contributor objects by their names.

File

includes/BiblioContributorUtility.inc, line 45
Helper class for handling Biblio Contributors.

Class

BiblioContributorUtility
@file Helper class for handling Biblio Contributors.

Code

public static function parseContributorName($full_name) {

  // @todo

  /*if (isset($contributor_array['auth_category']) && $contributor_array['auth_category'] == 5) {
        $contributor_array['firstname'] = '';
        $contributor_array['initials'] = '';
        $contributor_array['lastname'] = trim($contributor_array['name']);
        $contributor_array['prefix'] = '';
        $contributor_array['literal'] = 1;

        return $contributor_array;
      }*/
  $appellation = $prefix = $lastname = $firstname = $initials = '';

  // Remove unneeded spaces from full name.
  $full_name = trim($full_name);
  $full_name = preg_replace("/\\s{2,}/", ' ', $full_name);

  // Split full name by commas.
  $name_parts = explode(',', $full_name);

  // Count commas.
  $commas = count($name_parts) - 1;
  if (!$commas) {

    // No commas in full name.
    if (preg_match("/(.*) {([^\\\\].*)}/", $full_name, $matches) && !preg_match("/(.*) {\\\\.{.*}.*}/", $full_name, $matches2)) {

      // Complete last name enclosed in {...}, unless the string starts with a
      // backslash (\) because then it is probably a special latex-sign.
      // In the last case, any NESTED curly braces should also be taken into
      // account! so second clause rules out things such as 'a{\"{o}}'.
      $name_parts = explode(' ', $matches[1]);
      $lastname = $matches[2];
    }
    else {

      // The pattern is firstname-initials-lastname, such as 'George Bush',
      // 'George W. Bush', 'G W Bush' etc.
      // Split full name by spaces.
      $name_parts = explode(' ', $full_name);

      // Last element in the array should be the last name.
      // There shouldn't be a prefix if the name was entered correctly.
      $lastname = array_pop($name_parts);
    }
  }
  elseif ($commas == 1) {

    // There is 1 comma in full name, such as in 'Bush, George', 'Bush, G W',
    // 'Bush, George W', 'de la Bush, George W' etc.
    // Get last name and prefix separately.
    list($lastname, $prefix) = BiblioContributorUtility::getLastname(array_shift($name_parts));
  }
  else {

    // There are 2 commas in full name, such as in 'Bush, Jr. III, George W'.
    // Middle element in array is 'Jr.', 'IV' etc.
    $appellation = implode(' ', array_splice($name_parts, 1, 1));

    // Get last name and prefix separately.
    list($lastname, $prefix) = BiblioContributorUtility::getLastname(array_shift($name_parts));
  }

  // After removing the last name, prefix and appellation from the full name,
  // we are left with first name and initials, and perhaps last name prefix.
  $remainder = implode(' ', $name_parts);
  list($firstname, $initials, $prefix2) = BiblioContributorUtility::getFirstnameInitials($remainder);
  if (!empty($prefix2)) {

    // Found a prefix in remainder, add it to the last name prefix.
    $prefix .= $prefix2;
  }

  // Fill results in the array.
  $contributor_array['firstname'] = trim($firstname);
  $contributor_array['initials'] = substr(trim($initials), 0, 10);
  $contributor_array['lastname'] = trim($lastname);
  $contributor_array['prefix'] = trim($prefix);
  $contributor_array['suffix'] = trim($appellation);
  return $contributor_array;
}