Regular Expressions In Depth
preg_match with named groups, preg_replace_callback, preg_split and preg_quote. Use the u flag for Unicode and delimiters such as /.../.
Example
preg_match("/(?<y>\d{4})-(?<m>\d{2})/", "Date 2026-03-15", $m);
echo $m["y"], "/", $m["m"], "\n";
echo preg_replace_callback("/\d+/", fn($x) => $x[0] * 2, "a1 b22"), "\n";
print_r(preg_split("/[\s,]+/", "a, b c,d"));
echo preg_quote("a.b*c"), "\n";Result
2026/03
a2 b44
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
a\.b\*cExercise
Split with a regex.
("/,/", $s)Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.