Lesson content
Read, practise, then check your understanding
PHP uses PCRE through preg_match, preg_match_all, preg_replace, and related functions. Patterns use delimiters and optional modifiers.
Practical example
<?php
if (preg_match('/\A(?<slug>[a-z0-9]+(?:-[a-z0-9]+)*)\z/D', $input, $matches) !== 1) {
throw new InvalidArgumentException('Invalid slug');
}
Use the u modifier for UTF-8 semantics when required. Check errors, escape dynamic literal fragments with preg_quote, and avoid ambiguous nested quantifiers that can cause denial-of-service backtracking. Use parsers for complex grammars.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.