source = (string) $regex; } public function __toString() { return (string) $this->source; } /** * Run the regular expression against the given string. * * @since 9.16.0.0 * * @param string $str the string to run this regular expression against * * @return RegExMatch|null */ public function exec($str) { $index = null; $results = array(); preg_match($this->source, $str, $results, PREG_OFFSET_CAPTURE, $this->lastIndex); if ($results === null || count($results) === 0) { return null; } foreach ($results as &$result) { if ($result[1] !== -1) { // Only save the index if it hasn't been set yet if ($index === null) { $index = $result[1]; } $result = $result[0]; } else { $result = null; } } unset($result); $this->lastIndex += strlen($results[0]) + ($index - $this->lastIndex); $matches = new RegExMatch($results); $matches->index = isset($index) ? $index : 0; $matches->input = $str; return $matches; } }