PHP’s PCRE require the regular expression to be wrapped into delimiters that separate the pattern from optional modifiers. In this case the first non-alphanumeric character is used (i.e. '
) so the pattern is actually just (.*)<a (.*?)(.*) *href\=[
and the rest are treated as modifiers. And that is an invalid regular expression as the [
is not properly escaped and the rest are not valid modifiers neither.
As the others have already suggested, you can fix this by escaping any occurrence of the delimiter '
inside the regular expression or choose a different delimiter that does not appear in the regular expression.
But besides that, trying to parse HTML with regular expressions is very error prone. In you case using that many .*
will also result in a horrible performance behavior (it’s just due to how regular expressions are processed).
Better use a proper HTML parser that returns a DOM that can be queried like PHP’s DOM library:
$doc = new DomDocument();
$doc->loadHTML($str);
foreach ($doc->getElementsByTagName("a") as $a) {
if ($a->hasAttribute("href")) {
$href = trim($a->getAttribute("href"));
if (strtolower(substr($href, 0, 7)) === 'mailto:') {
$components = parse_url($href);
}
}
}