Regular Expressions
Prerequisites: Working with dates and times
PHP's preg_* functions wrap PCRE (Perl-Compatible Regular
Expressions) — patterns are delimited, usually with /.
$email = 'ada@example.com';
var_dump(preg_match('/^[\w.+-]+@[\w-]+\.[a-z]{2,}$/i', $email)); // int(1)
$result = preg_replace('/\s+/', ' ', "Too many spaces");
echo $result; // "Too many spaces"
preg_match_all('/\d+/', 'I have 3 cats and 12 fish', $matches);
print_r($matches[0]); // ["3", "12"]
In real applications, prefer PHP's built-in filter_var($email, FILTER_VALIDATE_EMAIL) for email validation specifically — it's
more thorough than a hand-written pattern.