You are here

public static function BiblioContributorUtility::getFirstnameInitials in Bibliography Module 7.3

Get first name, initials and last name prefix from a given string.

Parameters

$value: String containing first name, initials and perhaps a last name prefix.

Return value

Array with three separated strings: first name, initials and prefix.

1 call to BiblioContributorUtility::getFirstnameInitials()
BiblioContributorUtility::parseContributorName in includes/BiblioContributorUtility.inc
Get each part of a contributor's name separately.

File

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

Class

BiblioContributorUtility
@file Helper class for handling Biblio Contributors.

Code

public static function getFirstnameInitials($value) {
  $prefix = array();
  $firstname = array();
  $initials = array();

  // Split the given string to its parts.
  $parts = explode(' ', $value);
  foreach ($parts as $part) {
    if (ord(substr($part, 0, 1)) >= 97 && ord(substr($part, 0, 1)) <= 122) {

      // The part starts with a lowercase letter, so this is a last name prefix
      // such as 'den', 'von', 'de la' etc.
      $prefix[] = $part;
      continue;
    }
    if (preg_match("/[a-zA-Z]{2,}/", $part)) {

      // The part starts with an uppercase letter and contains two letters or
      // more and therefore is a first name such as 'George'.
      $firstname[] = $part;
      continue;
    }

    // The part contains only one uppercase letter and perhaps a dot and
    // therefore is an initial such as the W in 'George W. Bush'.
    $initials[] = trim(str_replace('.', ' ', $part));
  }

  // Convert arrays to strings.
  $prefix = !empty($prefix) ? implode(' ', $prefix) : '';
  $firstname = !empty($firstname) ? implode(' ', $firstname) : '';
  $initials = !empty($initials) ? implode(' ', $initials) : '';
  return array(
    $firstname,
    $initials,
    $prefix,
  );
}