function _name_format in Name Field 6
Same name and namespace in other branches
- 7 includes/name.parser.inc \_name_format()
1 call to _name_format()
- name_format in ./
name.module - This is the main function that formats a name from an array of components.
File
- includes/
name.parser.inc, line 120 - Provides the functionality and information about the Name module name parsing engine.
Code
function _name_format($name_components, $format = '', $settings = array(), $tokens = NULL) {
if (empty($format)) {
return '';
}
if (!isset($tokens)) {
$tokens = _name_generate_tokens($name_components);
}
// Nuetralise any escapped backslashes.
$format = str_replace('\\\\', "\t", $format);
$pieces = array();
$len = strlen($format);
$modifiers = '';
$conditions = '';
$depth = 0;
for ($i = 0; $i < strlen($format); $i++) {
$char = $format[$i];
$last_char = $i > 0 ? $format[$i - 1] : FALSE;
$next_char = $i < $len - 2 ? $format[$i + 1] : FALSE;
// Handle escaped letters.
if ($char == '\\') {
continue;
}
if ($last_char == '\\') {
$pieces[] = _name_format_add_component($char, $modifiers, $conditions);
continue;
}
switch ($char) {
case 'L':
case 'U':
case 'F':
case 'T':
case 'S':
case 'G':
$modifiers .= $char;
break;
case '=':
case '^':
case '|':
case '+':
case '-':
case '~':
$conditions .= $char;
break;
case '(':
case ')':
$remaining_string = substr($format, $i);
if ($char == '(' && ($closing_bracket = _name_format_closing_bracket_position($remaining_string))) {
$sub_string = _name_format($tokens, substr($format, $i + 1, $closing_bracket - 1), $settings, $tokens);
// Increment the counter past the closing bracket.
$i += $closing_bracket;
$pieces[] = _name_format_add_component($sub_string, $modifiers, $conditions);
}
else {
// Unmatched, add it.
$pieces[] = _name_format_add_component($char, $modifiers, $conditions);
}
break;
default:
if (array_key_exists($char, $tokens)) {
$char = $tokens[$char];
}
$pieces[] = _name_format_add_component($char, $modifiers, $conditions);
break;
}
}
$parsed_pieces = array();
for ($i = 0; $i < count($pieces); $i++) {
$component = $pieces[$i]['value'];
$conditions = $pieces[$i]['conditions'];
$last_component = $i > 0 ? $pieces[$i - 1]['value'] : FALSE;
$next_component = $i < count($pieces) - 1 ? $pieces[$i + 1]['value'] : FALSE;
if (empty($conditions)) {
$parsed_pieces[$i] = $component;
}
else {
// Modifier: Conditional insertion. Insert if both the surrounding tokens are not empty.
if (strpos($conditions, '+') !== FALSE && !empty($last_component) && !empty($next_component)) {
$parsed_pieces[$i] = $component;
}
// Modifier: Conditional insertion. Insert if the previous token is not empty.
if (strpos($conditions, '-') !== FALSE && !empty($last_component)) {
$parsed_pieces[$i] = $component;
}
// Modifier: Conditional insertion. Insert if the previous token is empty.
if (strpos($conditions, '~') !== FALSE && empty($last_component)) {
$parsed_pieces[$i] = $component;
}
// Modifier: Insert the token if the next token is empty.
if (strpos($conditions, '^') !== FALSE && empty($next_component)) {
$parsed_pieces[$i] = $component;
}
// Modifier: Insert the token if the next token is not empty.
// This overrides the above two settings.
if (strpos($conditions, '=') !== FALSE && !empty($next_component)) {
$parsed_pieces[$i] = $component;
}
// Modifier: Conditional insertion. Uses the previous token unless empty, otherwise insert this token.
if (strpos($conditions, '|') !== FALSE) {
if (empty($last_component)) {
$parsed_pieces[$i] = $component;
}
else {
unset($parsed_pieces[$i]);
}
}
}
}
return str_replace('\\\\', "\t", implode('', $parsed_pieces));
}