I need get
bbish name3 more
bbish name4 more
$p = '%<a\s+href="my-anchor-name3"\s*>(?:.*)</a>%im';
$s = 'some rubbish
<a href="my-anchor-name1">name</a>more rubbish
more rubbish<a href="my-anchor-name2">name2</a>more rubbish
more rubbish<a href="my-anchor-name3">name3</a>more rubbish
more rubbish<a href="my-anchor-name3">name4</a>more rubbish
more rubbish<a href="my-anchor-name5">name5</a>more rubbish';
$out = preg_match_all($p, $s, $matches, PREG_SET_ORDER);
what am I doing wrong?
You’re not instructing PHP to do what you have indicated that you want to do, is the main flaw.
Problems
atag is greedy;hrefvalue like that;Fix
Try this:
Output:
Live demo.
Further work
You may wish to further restrict what characters may be eaten up in those backreferences.
And if you don’t want to limit your
hrefvalues the way you are (and you’re doing it in quite a confusing way at present):Like this.
* The real answer here is that you should not be using regular expressions to parse HTML, which is a well-known fact. Marc has the solution that you should be using.