class HumanNameParser_Name in Bibliography Module 7
Same name and namespace in other branches
- 6.2 includes/Name.php \HumanNameParser_Name
Does cutting and matching stuff with a name string. Note that the string has to be UTF8-encoded.
Hierarchy
- class \HumanNameParser_Name
Expanded class hierarchy of HumanNameParser_Name
1 string reference to 'HumanNameParser_Name'
- HumanNameParser_Parser::setName in includes/
Parser.php - Sets name string and parses it. Takes Name object or a simple string (converts the string into a Name obj), parses and loads its constituant parts.
File
- includes/
Name.php, line 7
View source
class HumanNameParser_Name {
private $str;
/**
*
*/
public function __construct($str) {
$this
->setStr($str);
}
/**
* Checks encoding, normalizes whitespace/punctuation, and sets the name string.
*
* @param string $str
* a utf8-encoding string.
*
* @return Bool True on success
*/
public function setStr($str) {
if (!drupal_validate_utf8($str)) {
throw new Exception("Name is not encoded in UTF-8");
}
$this->str = $str;
$this
->norm();
return TRUE;
}
/**
*
*/
public function getStr() {
return $this->str;
}
/**
* 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.
*
* @param string $regex
* matches the part of the namestring to chop off.
* @param int $submatchIndex
* which of the parenthesized submatches to use.
* @param string $regexFlags
* optional regex flags.
*
* @return string the part of the namestring that got chopped off
*/
public function chopWithRegex($regex, $submatchIndex = 0, $regexFlags = '') {
// Unicode + case-insensitive.
$regex = $regex . "ui" . $regexFlags;
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 '';
}
}
/**
* Flips the front and back parts of a name with one another.
* Front and back are determined by a specified character somewhere in the
* middle of the string.
*
* @param string $flipAroundChar
* the character(s) demarcating the two halves you want to flip.
*
* @return Bool True on success.
*/
public function flip($flipAroundChar) {
$substrings = preg_split("/{$flipAroundChar}/u", $this->str);
if (count($substrings) == 2) {
$this->str = $substrings[1] . " " . $substrings[0];
$this
->norm();
}
elseif (count($substrings) > 2) {
throw new Exception("Can't flip around multiple '{$flipAroundChar}' characters in: '{$this->str}'.");
}
// If there's 1 or 0 $flipAroundChar found.
return TRUE;
}
/**
* Removes extra whitespace and punctuation from $this->str
* Strips whitespace chars from ends, strips redundant whitespace, converts whitespace chars to " ".
*
* @return Bool True on success
*/
private function norm() {
$this->str = preg_replace("#^\\s*#u", "", $this->str);
$this->str = preg_replace("#\\s*\$#u", "", $this->str);
$this->str = preg_replace("#\\s+#u", " ", $this->str);
$this->str = preg_replace("#,\$#u", " ", $this->str);
return TRUE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
HumanNameParser_Name:: |
private | property | ||
HumanNameParser_Name:: |
public | function | 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. | |
HumanNameParser_Name:: |
public | function | Flips the front and back parts of a name with one another. Front and back are determined by a specified character somewhere in the middle of the string. | |
HumanNameParser_Name:: |
public | function | ||
HumanNameParser_Name:: |
private | function | Removes extra whitespace and punctuation from $this->str Strips whitespace chars from ends, strips redundant whitespace, converts whitespace chars to " ". | |
HumanNameParser_Name:: |
public | function | Checks encoding, normalizes whitespace/punctuation, and sets the name string. | |
HumanNameParser_Name:: |
public | function |