PHP Regular Expression Reference
A pattern API can report match, no match or an execution error. Keep those states distinct when validating a field.
How it works
preg_match and preg_match_all have different count/result contracts. Replacement and splitting APIs also have their own limits and failure returns. Dynamic literal text belongs through preg_quote rather than being treated as a regex fragment. For untrusted input, choose bounded formats and input lengths; a clever pattern that takes excessive time is a poor validation boundary.
Run it locally
Run this complete source locally with PHP 8.2 or newer. Save it using the filename shown and run the command below. PHP is not currently available in Bhylu’s browser playground. Apache and MySQL are not needed for an ordinary CLI example.
php -l ref-patterns.php
php ref-patterns.phpDownload this source as a local PHP lab file. Keep lab files separate from the Bhylu project and production data.
Example
<?php
declare(strict_types=1);
$literal = "Tool (small)";
$pattern = "/\A" . preg_quote($literal,"/") . "\z/";
echo preg_match($pattern,$literal) === 1 ? "Literal matched\n" : "No match\n";
Expected result
Literal matchedThe parentheses are matched as literal characters instead of creating a grouping construct. Anchors require the complete input to match. The code still checks for a match explicitly rather than assuming every non-zero-like value means the same state; production callers should inspect false and diagnostics for a pattern execution error.
Contracts to check
| API or term | Important boundary |
|---|---|
| preg_match, preg_match_all | Read match-count and false/error contracts separately. |
| preg_replace, preg_replace_callback | Replacement syntax and callback return values differ. |
| preg_split, preg_grep | Check result shape, limits and error handling. |
| preg_quote | Escape literal text for a chosen regex delimiter. |
| preg_last_error, preg_last_error_msg | Inspect an execution failure rather than treating it as normal rejection. |
Related API names
This alphabetic inventory helps locate related calls. Availability was checked in the review runtime; it is not a recommendation to use every API or a claim that every overload/function was executed. Read the official entry for version, extension, failure and security details.
| Name | Review-runtime status |
|---|---|
preg_filter() | Available in the PHP review runtime |
preg_grep() | Available in the PHP review runtime |
preg_last_error() | Available in the PHP review runtime |
preg_match() | Available in the PHP review runtime |
preg_match_all() | Available in the PHP review runtime |
preg_quote() | Available in the PHP review runtime |
preg_replace() | Available in the PHP review runtime |
preg_replace_callback() | Available in the PHP review runtime |
preg_replace_callback_array() | Available in the PHP review runtime |
preg_split() | Available in the PHP review runtime |
Check the official PHP manual for detailed version and API contracts.