Tools
Log in
English

Learn to codePHP

PHP preg regex tester

PHP 8.3Runs in your browser

Test preg_match, preg_match_all, preg_replace and preg_split in PHP style.

PHP preg regex tester

Match

No.NameValueOffset (bytes)

print_r

Pattern explained

    How to use

    1. 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 a print_r style result.
    2. The pattern explainer gives one line per token.
    3. 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 add u for 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 hit pcre.backtrack_limit and return null.
    • Atomic groups (?>...), possessive quantifiers like *+, \K and 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.