You are here

public function HumanNameParser_Name::chopWithRegex in Bibliography Module 6.2

Same name and namespace in other branches
  1. 7 includes/Name.php \HumanNameParser_Name::chopWithRegex()

Uses a regex to chop off and return part of the namestring There are two parts: first, it returns the matched substring, and then it removes that substring from $this->str and normalizes.

Parameters

string $regex matches the part of the namestring to chop off:

integer $submatchIndex which of the parenthesized submatches to use:

string $regexFlags optional regex flags:

Return value

string the part of the namestring that got chopped off

File

includes/Name.php, line 47

Class

HumanNameParser_Name
Does cutting and matching stuff with a name string. Note that the string has to be UTF8-encoded.

Code

public function chopWithRegex($regex, $submatchIndex = 0, $regexFlags = '') {
  $regex = $regex . "ui" . $regexFlags;

  // unicode + case-insensitive
  preg_match($regex, $this->str, $m);
  $subset = isset($m[$submatchIndex]) ? $m[$submatchIndex] : '';
  if ($subset) {
    $this->str = preg_replace($regex, ' ', $this->str, -1, $numReplacements);
    if ($numReplacements > 1) {
      throw new Exception("The regex being used to find the name: '{$this->str}' has multiple matches.");
    }
    $this
      ->norm();
    return $subset;
  }
  else {
    return '';
  }
}