You are here

public static function MatchBase::findAll in Password Strength 8.2

Find all occurences of regular expression in a string.

Parameters

string $string: String to search.

string $regex: Regular expression with captures.

Return value

array Array of capture groups. Captures in a group have named indexes: 'begin', 'end', 'token'. e.g. fishfish /(fish)/ array( array( array('begin' => 0, 'end' => 3, 'token' => 'fish'), array('begin' => 0, 'end' => 3, 'token' => 'fish') ), array( array('begin' => 4, 'end' => 7, 'token' => 'fish'), array('begin' => 4, 'end' => 7, 'token' => 'fish') ) )

File

src/MatchBase.php, line 36

Class

MatchBase

Namespace

Drupal\password_strength

Code

public static function findAll($string, $regex) {
  $count = preg_match_all($regex, $string, $matches, PREG_SET_ORDER);
  if (!$count) {
    return array();
  }
  $pos = 0;
  $groups = array();
  foreach ($matches as $group) {
    $captureBegin = 0;
    $match = array_shift($group);
    $matchBegin = strpos($string, $match, $pos);
    $captures = array(
      array(
        'begin' => $matchBegin,
        'end' => $matchBegin + strlen($match) - 1,
        'token' => $match,
      ),
    );
    foreach ($group as $capture) {
      $captureBegin = strpos($match, $capture, $captureBegin);
      $captures[] = array(
        'begin' => $matchBegin + $captureBegin,
        'end' => $matchBegin + $captureBegin + strlen($capture) - 1,
        'token' => $capture,
      );
    }
    $groups[] = $captures;
    $pos += strlen($match) - 1;
  }
  return $groups;
}