Skip to content

Commit eeea3df

Browse files
committed
Add fuzzyMatch to StringProcessor
1 parent 42b28e6 commit eeea3df

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/Libraries/Core/StringProcessor.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44

55
class StringProcessor
66
{
7+
/**
8+
* Matches a pattern to a fuzzy subject, e.g. 'api.*' should match 'api.enabled'
9+
*
10+
* @param string $pattern The pattern that the subject should match, e.g. 'api.*'
11+
* @param string $subject The subject that is being compared, e.g. 'api.enabled'
12+
* @return boolean Whether the string matches.
13+
*/
14+
public static function fuzzyMatch(string $pattern, string $subject): bool
15+
{
16+
$pattern = \preg_replace_callback('/([^*])/', 'preg_quote', $pattern);
17+
$pattern = \str_replace('*', '.*', $pattern);
18+
return (bool) \preg_match('/^' . $pattern . '$/i', $subject);
19+
}
20+
721
/**
822
* Checks whether the current User Agent is a GUI web browser known by a common string, e.g. Firefox.
923
*

0 commit comments

Comments
 (0)