Anchored regex without fear

A regular expression in PHP is a pattern string handed to a PCRE function: it tests text, extracts the matched part or edits it. The four functions and rules on this page cover most of the work.

preg_match

Function preg_match tests a string against a pattern and returns 1 on success and 0 on failure. Anchors lock the test to the full string so partial hits do not pass.

Caret marks the start and dollar marks the end, so a pattern with both checks the whole value from first to last character.

<?php $text = "order-42"; if (preg_match("/^order-[0-9]+$/", $text, $m)) { echo $m[0], PHP_EOL; }

php match.php

order-42

The third argument receives the match: $m[0] is the whole value that matched and $m[1] would be the first group.

preg_replace

Function preg_replace returns a new string with every match replaced. The original value stays unchanged and the result holds the edits.

Use a character class for digits or letters when the exact text varies but the shape stays the same.

<?php $text = "hi 2026-10-06"; echo preg_replace("/[0-9]+/", "#", $text), PHP_EOL;

php replace.php

hi #-#-#

Each run of digits became one hash, because the class with a + takes the whole run at once. The original $text still holds the date.

Delimiters

Every pattern needs delimiters around the core expression. The common choice is / but hash and tilde work the same way.

Pick a delimiter that does not appear in the pattern itself. That choice keeps the pattern short and easy to read.

<?php $text = "path to file"; var_dump((bool) preg_match("#^path#", $text)); var_dump((bool) preg_match("~file$~", $text));

php delimiters.php

bool(true)
bool(true)

The hash pattern tests the start of the text and the tilde pattern asks for file at its end, and both pass in the same way. The delimiter is a wrapper, so its shape changes nothing but the characters the pattern has to escape.

Escape with preg_quote

Never paste user text straight into a pattern because dots and brackets have special meaning. Function preg_quote escapes every special character for you.

Pass the delimiter as the second argument so it gets escaped as well, then add anchors around the quoted part.

<?php $word = "price (usd)"; $pattern = "/^" . preg_quote($word, "/") . "$/"; var_dump((bool) preg_match($pattern, "price (usd)")); var_dump((bool) preg_match("/^price (usd)$/", "price (usd)"));

php safe.php

bool(true)
bool(false)

The quoted pattern takes the brackets as text and matches. The hand-written one reads them as a group, so it looks for price usd with a space instead and finds nothing: that is the bug preg_quote removes.

Exercises

  1. Write a pattern that accepts only a date in the shape 2026-10-06 and prove it rejects 2026-10.
  2. Replace every run of spaces in a sentence with a single space and print the result.
  3. Show that a pattern with # delimiters refuses an unclosed pattern.
  4. Quote an email address with preg_quote and match it against itself.
  5. Print the first group of a pattern with parentheses around the digits of an order number.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does preg_match() return when the pattern matches?

    Answer

    1

    preg_match() returns 1 for a match and 0 for no match; the matched text goes into the third argument.

  2. What does the caret ^ anchor in a pattern?

    Answer

    The start of the text

    The caret binds the rest of the pattern to the beginning of the subject.

  3. What does the dollar $ anchor in a pattern?

    Answer

    The end of the text

    The dollar binds the part before it to the end of the subject.

  4. Which function escapes regex metacharacters in a literal string?

    Answer

    preg_quote()

    preg_quote() puts a backslash before every character the pattern engine would read as syntax.

  5. What does this program print? <?php $text = "order-42"; if (preg_match("/^order-[0-9]+$/", $text, $m)) { echo $m[0]; }

    Answer

    order-42

    Index 0 of the match array is the whole matched text, from the first to the last character.

  6. What does this program print? <?php $text = "hi 2026-10-06"; echo preg_replace("/[0-9]+/", "#", $text);

    Answer

    hi #-#-#

    The class with + takes each run of digits as one match, so each date part becomes a single hash.

  7. What does this program print? <?php $text = "path to file"; var_dump((bool) preg_match("#^path#", $text)); var_dump((bool) preg_match("~file$~", $text));

    Answer

    bool(true) / bool(true)

    The text starts with path and ends with file, so both patterns match and the two delimiters behave the same.

  8. What does this program print? <?php var_dump((bool) preg_match("/^price (usd)$/", "price (usd)"));

    Answer

    bool(false)

    The brackets are a group in a pattern, so the text it looks for is price usd with a space, not the literal brackets.

  9. What must sit around the core expression of every PHP pattern?

    Answer

    A pair of delimiters such as slashes

    PHP reads the first character as the delimiter and looks for the same character to close the pattern.

  10. What does this program print? <?php $word = "price (usd)"; $pattern = "/^" . preg_quote($word, "/") . "$/"; var_dump((bool) preg_match($pattern, "price (usd)"));

    Answer

    bool(true)

    preg_quote() turns the brackets into literal characters, so the pattern matches the text that built it.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev