PHP preg regex tester
PHP 8.3Runs in your browserTest preg_match, preg_match_all, preg_replace and preg_split in PHP style.
PHP preg regex tester
Match
| No. | Name | Value | Offset (bytes) |
|---|
print_r
Pattern explained
How to use
- Write the pattern the PHP way, with delimiters and trailing flags (for example
/(\d+)-(\d+)/u). Pick a function (preg_match,preg_match_all,preg_replace,preg_split) to see matches highlighted, a group table and aprint_rstyle result. - The pattern explainer gives one line per token.
- The instant result is simulated with the browser JavaScript engine. Features that only PHP (PCRE) has are flagged, and Check with browser PHP runs the real
preg_*call and compares.
Good to know
- Without
u, PHP works on bytes, so one Korean letter matches three.characters. Always addufor non-ASCII text. - Offsets are byte counts, as in PHP.
- Heavy patterns that backtrack a lot, such as
(a+)+$, stop after 1 second. In PHP they hitpcre.backtrack_limitand returnnull. - Atomic groups
(?>...), possessive quantifiers like*+,\Kand recursion(?R)do not exist in JavaScript, so check those with PHP.
Questions people ask
Why is Korean text cut in odd places?
Without the u flag PHP matches bytes, so one Korean letter counts as three pieces. Add u at the end of the pattern.
How is it different from JavaScript regex?
PHP uses PCRE, which adds atomic groups, possessive quantifiers, \K and recursion. Such features are flagged and can be checked with real PHP.
preg_match returns false or null.
Either the pattern is invalid or it hit the backtracking limit. preg_last_error_msg() tells you which.