You are here

function biblio_parse_author in Bibliography Module 6.2

Same name and namespace in other branches
  1. 6 biblio.contributors.inc \biblio_parse_author()
  2. 7 includes/biblio.contributors.inc \biblio_parse_author()
  3. 7.2 includes/biblio.contributors.inc \biblio_parse_author()

Parses an author name into its component parts.

This function is partly based on a collection of PHP classes to manipulate bibtex files and other work at http://bibliophile.sourceforge.net.

That work was released through http://bibliophile.sourceforge.net under the GPL licence. Do whatever you like with this -- some credit to the author(s) would be appreciated. If you make improvements, please consider contacting the administrators at bibliophile.sourceforge.net so that your improvements can be added to the release package.

Mark Grimshaw 2004/2005 http://bibliophile.sourceforge.net

28/04/2005 - Mark Grimshaw. Efficiency improvements.

11/02/2006 - Daniel Reidsma. Changes to preg_matching to account for Latex characters in names such as {\"{o}}.

Parameters

string|array $contributor: A string representing a name or an array with

$category_id: (optional)

Return value

array|false FALSE if there was an error; otherwise, an associative array for the parsed parts of a name, including at minimum the keys:

  • name:
  • firstname:
  • initials:
  • lastname:
  • prefix:
  • md5:
7 calls to biblio_parse_author()
BiblioContributorUnitTest::testBiblioParseAuthor in tests/contributor.test
biblio_authors_add_etal in includes/biblio.contributors.inc
Adds additional author named "et al" to the end of the author array.
biblio_calculate_contributor_hash in includes/biblio.contributors.inc
Creates an md5 hash string to ease contributor comparison logic.
biblio_parse_contributors in includes/biblio.contributors.inc
Parses array of contributors and augments with additional information.
csl_name::render in modules/CiteProc/CSL.inc

... See full list

File

includes/biblio.contributors.inc, line 552
Functions related to contributors in Drupal biblio module.

Code

function biblio_parse_author($contributor, $category_id = 0) {
  if (is_string($contributor)) {
    $author_array = array();
    $author_array['name'] = $contributor;
  }
  elseif (is_array($contributor) && isset($contributor['name'])) {
    $author_array = $contributor;
  }
  else {
    return FALSE;
  }
  if ($category_id == 5) {
    $author_array['firstname'] = '';
    $author_array['initials'] = '';
    $author_array['lastname'] = trim($author_array['name']);
    $author_array['prefix'] = '';

    // @todo: set suffix to empty string?
  }
  else {
    $value = trim($author_array['name']);
    $appellation = $prefix = $surname = $firstname = $initials = '';
    $prefix = "";
    $value = preg_replace("/\\s{2,}/", ' ', $value);

    // replace multiple white space by single space
    $author = explode(",", $value);
    $size = sizeof($author);

    // If only one element to author array, no commas were found.  Therefore,
    // the name is something like Mark Grimshaw | Mark Nicholas Grimshaw |
    // Mark N. Grimshaw | M N Grimshaw | or such.
    if ($size == 1) {

      // Is complete surname enclosed in {...}, unless the string starts with a backslash (\) because then it is
      // probably a special latex-sign..
      // 2006.02.11 DR: in the last case, any NESTED curly braces should also be taken into account! so second
      // clause rules out things such as author="a{\"{o}}"
      //
      if (preg_match("/(.*){([^\\\\].*)}/", $value, $matches) && !preg_match("/(.*){\\\\.{.*}.*}/", $value, $dummy)) {
        $author = explode(" ", $matches[1]);
        $surname = $matches[2];
      }
      else {
        $author = explode(" ", $value);

        // Last of element of array is surname (no prefix if entered correctly).
        $surname = array_pop($author);
      }
    }
    elseif ($size == 2) {

      // First element of array is surname (perhaps with a prefix).
      list($surname, $prefix) = _biblio_extract_surname_parts(array_shift($author));
    }
    else {

      // Middle element of array is 'Jr.', 'IV', etc.
      $appellation = implode(' ', array_splice($author, 1, 1));

      // First element of array is surname (perhaps with prefix).
      list($surname, $prefix) = _biblio_extract_surname_parts(array_shift($author));
    }
    $remainder = implode(" ", $author);
    list($firstname, $initials, $prefix2) = _biblio_extract_firstname_initials($remainder);
    if (!empty($prefix2)) {
      $prefix .= $prefix2;
    }
    $author_array['firstname'] = trim($firstname);
    $author_array['initials'] = trim($initials);
    $author_array['lastname'] = trim($surname);
    $author_array['prefix'] = trim($prefix);
    $author_array['suffix'] = trim($appellation);
  }
  $author_array['md5'] = biblio_calculate_contributor_hash($author_array);
  return $author_array;
}