Appearance
Regular Expressions
Prioritize regex readability. For guidance, refer to Writing better Regular Expressions in PHP.
Use DEFINE for recurring patterns and sprintf for reusable definitions:
php
final class RegexHelper
{
/** @return array<string, string> */
public function images(string $htmlContent): array
{
$pattern = '
(?'image' # Capture named group
(?P>img) # Recurse img subpattern from definitions
)
';
preg_match_all($this->createRegex($pattern), $htmlContent, $matches);
return $matches['image'];
}
private function createRegex(string $pattern): string
{
return sprintf($this->getDefinitions(), preg_quote($pattern, '~'));
}
private function getDefinitions(): string
{
return "~
(?(DEFINE) # Allows defining reusable patterns
(?'attr'(?:\s[^>]++)?) # Capture HTML attributes
(?'img'<img(?P>params)>) # Capture HTML img tag with its attributes
)
%s #Allows adding dynamic regex using sprintf
~ix";
}
}Use Regex101 for testing patterns.
TIP
There is a less popular, hidden PHP germ sscanf function that can be used for parsing strings and simplify your code in some cases.