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
- You did not create an array into which to deposit the matches;
- You're not capturing any backreferences;
- Your capture inside the
a
tag is greedy;
- I suspect that you don't really want to restrict your
href
value like that;
- Your HTML input is very restricted, because you're using regular expressions to parse HTML.... grrrrr!! *
Fix
Try this:
<?php
$matches = Array();
$p = '%(.{0,5})<a\s+href="my-anchor-name3"\s*>(.*?)</a>(.{0,5})%imm';
$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);
print_r($matches);
?>
Output:
Array
(
[0] => Array
(
[0] => bbish<a href="my-anchor-name3">name3</a>more
[1] => bbish
[2] => name3
[3] => more
)
[1] => Array
(
[0] => bbish<a href="my-anchor-name3">name4</a>more
[1] => bbish
[2] => name4
[3] => more
)
)
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 href
values the way you are (and you're doing it in quite a confusing way at present):
$p = '%(.{0,5})<a\s+href="my-anchor-name\d+"\s*>(.*?)</a>(.{0,5})%imm';
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.