danielebarchiesi@6: setStr($str); danielebarchiesi@6: } danielebarchiesi@6: danielebarchiesi@6: /** danielebarchiesi@6: * Checks encoding, normalizes whitespace/punctuation, and sets the name string. danielebarchiesi@6: * danielebarchiesi@6: * @param String $str a utf8-encoding string. danielebarchiesi@6: * @return Bool True on success danielebarchiesi@6: */ danielebarchiesi@6: public function setStr($str) danielebarchiesi@6: { danielebarchiesi@6: if (!drupal_validate_utf8($str)){ danielebarchiesi@6: throw new Exception("Name is not encoded in UTF-8"); danielebarchiesi@6: } danielebarchiesi@6: $this->str = $str; danielebarchiesi@6: $this->norm(); danielebarchiesi@6: return true; danielebarchiesi@6: } danielebarchiesi@6: danielebarchiesi@6: public function getStr() danielebarchiesi@6: { danielebarchiesi@6: return $this->str; danielebarchiesi@6: } danielebarchiesi@6: danielebarchiesi@6: danielebarchiesi@6: /** danielebarchiesi@6: * Uses a regex to chop off and return part of the namestring danielebarchiesi@6: * There are two parts: first, it returns the matched substring, danielebarchiesi@6: * and then it removes that substring from $this->str and normalizes. danielebarchiesi@6: * danielebarchiesi@6: * @param string $regex matches the part of the namestring to chop off danielebarchiesi@6: * @param integer $submatchIndex which of the parenthesized submatches to use danielebarchiesi@6: * @param string $regexFlags optional regex flags danielebarchiesi@6: * @return string the part of the namestring that got chopped off danielebarchiesi@6: */ danielebarchiesi@6: public function chopWithRegex($regex, $submatchIndex = 0, $regexFlags = '') danielebarchiesi@6: { danielebarchiesi@6: $regex = $regex . "ui" . $regexFlags; // unicode + case-insensitive danielebarchiesi@6: preg_match($regex, $this->str, $m); danielebarchiesi@6: $subset = (isset($m[$submatchIndex])) ? $m[$submatchIndex] : ''; danielebarchiesi@6: danielebarchiesi@6: if ($subset){ danielebarchiesi@6: $this->str = preg_replace($regex, ' ', $this->str, -1, $numReplacements); danielebarchiesi@6: if ($numReplacements > 1){ danielebarchiesi@6: throw new Exception("The regex being used to find the name: '$this->str' has multiple matches."); danielebarchiesi@6: } danielebarchiesi@6: $this->norm(); danielebarchiesi@6: return $subset; danielebarchiesi@6: } danielebarchiesi@6: else { danielebarchiesi@6: return ''; danielebarchiesi@6: } danielebarchiesi@6: } danielebarchiesi@6: danielebarchiesi@6: /* danielebarchiesi@6: * Flips the front and back parts of a name with one another. danielebarchiesi@6: * Front and back are determined by a specified character somewhere in the danielebarchiesi@6: * middle of the string. danielebarchiesi@6: * danielebarchiesi@6: * @param String $flipAroundChar the character(s) demarcating the two halves you want to flip. danielebarchiesi@6: * @return Bool True on success. danielebarchiesi@6: */ danielebarchiesi@6: public function flip($flipAroundChar) danielebarchiesi@6: { danielebarchiesi@6: $substrings = preg_split("/$flipAroundChar/u", $this->str); danielebarchiesi@6: if (count($substrings) == 2){ danielebarchiesi@6: $this->str = $substrings[1] . " " . $substrings[0]; danielebarchiesi@6: $this->norm(); danielebarchiesi@6: } danielebarchiesi@6: else if (count($substrings) > 2) { danielebarchiesi@6: throw new Exception("Can't flip around multiple '$flipAroundChar' characters in: '$this->str'."); danielebarchiesi@6: } danielebarchiesi@6: return true; // if there's 1 or 0 $flipAroundChar found danielebarchiesi@6: } danielebarchiesi@6: danielebarchiesi@6: /** danielebarchiesi@6: * Removes extra whitespace and punctuation from $this->str danielebarchiesi@6: * Strips whitespace chars from ends, strips redundant whitespace, converts whitespace chars to " ". danielebarchiesi@6: * danielebarchiesi@6: * @return Bool True on success danielebarchiesi@6: */ danielebarchiesi@6: private function norm() danielebarchiesi@6: { danielebarchiesi@6: $this->str = preg_replace( "#^\s*#u", "", $this->str ); danielebarchiesi@6: $this->str = preg_replace( "#\s*$#u", "", $this->str ); danielebarchiesi@6: $this->str = preg_replace( "#\s+#u", " ", $this->str ); danielebarchiesi@6: $this->str = preg_replace( "#,$#u", " ", $this->str ); danielebarchiesi@6: return true; danielebarchiesi@6: } danielebarchiesi@6: } danielebarchiesi@6: ?>