Anchored regex without fear

Contents
preg_match
preg_replace
Delimiters
Escape with preg_quote

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.

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

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.

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

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.

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

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.

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

Next: Multibyte strings

Article author: Arthur Isaev

Related articles
Development with PHP
PHP course: zero to hero in 50 lessons
What is PHP and where it runs
Install PHP 8.5 and run your first command
Your first PHP script
Comments in PHP
Variables and constants
Types in PHP
Strings in PHP
Numbers in PHP
Operators
if and else
switch and match
Ternary, null coalescing and nullsafe
Loops
Arrays
Associative arrays
Functions
Variable scope
include and require
Namespaces
Composer and autoloading
Everyday string functions
Dates and time
JSON in PHP
Reading and writing files
Errors: read, reproduce, fix
Classes and objects
Constructors and promotion
Inheritance
Interfaces and traits
Enums
Magic methods
Attributes
Exceptions
Strict types
Closures and arrow functions
Generators with yield
Forms: GET and POST
HTTP: headers and status codes
Cookies
Sessions
Login with sessions
JSON REST endpoint
Database with PDO
Uploads and .htaccess
Lint like a pro
Clean functions
Debugging
Modern PHP tour: 5 to 8.4
PHP 8.5 for busy developers
Hero roadmap: the whole course on one page
Install PHP 8.5 on Windows 11
Install PHP 8.5 on Ubuntu
Install PHP 8.5 on Rocky Linux
Install PHP 8.5 on macOS
Install PHP 8.5 on FreeBSD
PHP 8.5 in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: