Skip to content

Commit 65b1f14

Browse files
committed
Add Re::hasMatch()
1 parent 7daad4c commit 65b1f14

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/common/Re.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ public static function pos(string $string, string $pattern, int $offset = 0): ?i
108108
return $matches[0][1];
109109
}
110110

111+
public static function hasMatch(string $string, string $pattern, int $offset = 0): bool
112+
{
113+
if ($offset > strlen($string)) {
114+
return false;
115+
}
116+
117+
$result = preg_match($pattern, $string, $matches, $offset);
118+
if ($result === false) {
119+
$error = preg_last_error() ?: 0;
120+
121+
throw new RegexpException($error);
122+
} elseif ($result === 0) {
123+
return false;
124+
}
125+
126+
return true;
127+
}
128+
111129
/**
112130
* @param int $flags PREG_OFFSET_CAPTURE|PREG_UNMATCHED_AS_NULL
113131
* @return string[]|null

0 commit comments

Comments
 (0)