You are here

protected function NameFormatParser::closingBracketPosition in Name Field 8

Helper function to put out the first matched bracket position.

Parameters

string $string: Accepts strings in the format, ^ marks the matched bracket.

i.e. '(xxx^)xxx(xxxx)xxxx' or '(xxx(xxx(xxxx))xxx^)'.

Return value

mixed The closing bracket position or FALSE if not found.

1 call to NameFormatParser::closingBracketPosition()
NameFormatParser::format in src/NameFormatParser.php
Formats an array of name components into the supplied format.

File

src/NameFormatParser.php, line 347

Class

NameFormatParser
Converts a name from an array of components into a defined format.

Namespace

Drupal\name

Code

protected function closingBracketPosition($string) {

  // Simplify the string by removing escaped brackets.
  $depth = 0;
  $string = str_replace([
    '\\(',
    '\\)',
  ], [
    '__',
    '__',
  ], $string);
  for ($i = 0; $i < strlen($string); $i++) {
    $char = $string[$i];
    if ($char == '(') {
      $depth++;
    }
    elseif ($char == ')') {
      $depth--;
      if ($depth == 0) {
        return $i;
      }
    }
  }
  return FALSE;
}