Skip to content

Commit

Permalink
Merge pull request #33 from bolt/feature/is-valid-regex
Browse files Browse the repository at this point in the history
Add `Str::isValidRegex` test
  • Loading branch information
bobdenotter authored Feb 4, 2021
2 parents 3c5833a + 91b3e57 commit fd85000
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,4 +622,15 @@ public static function cleanWhitespace(string $str, string $charlist = " \t\n\r\
{
return trim(preg_replace('/[\t\n\r\s]+/', ' ', $str), $charlist);
}

/**
* Tests a string as a Regular Expression (regex)
* @return bool true if valid.
*/
public static function isValidRegex(string $regex): bool
{
@preg_match($regex, '');

return preg_last_error() === PREG_NO_ERROR;
}
}

0 comments on commit fd85000

Please sign in to comment.