array('format' => 'title first_name last_name', 'sort_by' => 'first_name')); } $search = array('first_name', 'last_name', 'title'); $replacement = array('%f', '%l', '%t'); foreach (array_keys($conventions) as $key) { $conventions[$key]['format'] = _api_validate_person_name_format(_api_clean_person_name(str_replace('%', ' %', str_ireplace($search, $replacement, $conventions[$key]['format'])))); $conventions[$key]['sort_by'] = strtolower($conventions[$key]['sort_by']) != 'last_name' ? true : false; } } switch ($type) { case 'format': return is_string($conventions[$language]['format']) ? $conventions[$language]['format'] : '%t %f %l'; case 'sort_by': return is_bool($conventions[$language]['sort_by']) ? $conventions[$language]['sort_by'] : true; } return null; } /** * Replaces non-valid formats for person names with the default (English) format. * @param string $format The input format to be verified. * @return bool Returns the same format if is is valid, otherwise returns a valid English format. */ function _api_validate_person_name_format($format) { if (empty($format) || strpos($format, '%f') === false || strpos($format, '%l') === false) { return '%t %f %l'; } return $format; } /** * Removes leading, trailing and duplicate whitespace and/or commas in a full person name. * Cleaning is needed for the cases when not all parts of the name are available or when the name is constructed using a "dirty" pattern. * @param string $person_name The input person name. * @return string Returns cleaned person name. */ function _api_clean_person_name($person_name) { return preg_replace(array('/\s+/', '/, ,/', '/,+/', '/^[ ,]/', '/[ ,]$/'), array(' ', ', ', ',', '', ''), $person_name); }