Skip to content
Justice Tech School

Regular Expressions

preg_match tests a pattern, preg_replace replaces and preg_split splits.

Example
echo preg_match("/^\d{3}-\d{4}$/", "555-1234"), "\n";
echo preg_replace("/\s+/", " ", "a   b    c"), "\n";
print_r(preg_split("/[,;]/", "a,b;c"));

Result

1
a b c
Array
(
    [0] => a
    [1] => b
    [2] => c
)

Exercise

Test a pattern.

("/a/", "cat")

Quiz Test

What does preg_match return on success?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.