I want to match all text following >
, and optionally match links on the same line:
preg_match('#(href="([^"]*))?.*>(.*)#', '<a href="world.html">Hello', $m);
print_r($m);
Input examples:
<a href="#catch-me" style="nice">Capture this text
This text should be ignored <a href="#me-too">Other text to capture
<p>This line has no link, but should be matched anyway.
Expected result:
[2] => world.html
[3] => Hello
Actual result:
[2] =>
[3] => Hello
It works if I remove the question mark, but then the link obviously isn't optional anymore.
Why is this happening and how do I fix it?