-1

i have some dynamic link like this

<a href="names.php?action=names&amp;name=1">jehan</a> 
<a href="names.php?action=names&amp;name=2">roy</a>

I want to extract just jehan and roy from those links.I have tried this

preg_match_all("'(.*?)</a>'", $check, $match);

I have no idea how to do this properly :(

esqew
  • 39,199
  • 27
  • 87
  • 126

3 Answers3

0
/<a [^>]+>(.*)<\/a>/

http://regexr.com/3abv0

There are more elegant ways to work this out. Simple HTML DOM, for example.

Bitwise Creative
  • 3,956
  • 5
  • 28
  • 34
0

This is what I would use to parse text from HTML links. Any further HTML parsing needs I had I would switch to a parsing library for PHP.

Regex

(?<=\>)\w+(?=\<\/a\>)

REGEX101

Usage

$re = "/(?<=\\>)\\w+(?=\\<\\/a\\>)/m"; 
$str = "<a href=\"names.php?action=names&amp;name=1\">jehan</a> \n<a href=\"names.php?action=names&amp;name=2\">roy</a>"; 

preg_match_all($re, $str, $matches);
MattSizzle
  • 3,125
  • 1
  • 20
  • 42
0
<a [^>]*>\K[^<]*(?=<\/a>)

You can try this to grab the links.See demo.

https://www.regex101.com/r/oI2jF9/3

$re = "/<a [^>]*>\\K[^<]*(?=<\\/a>)/m";
$str = "<a href=\"names.php?action=names&amp;name=1\">jehan</a> \n<a href=\"names.php?action=names&amp;name=2\">roy</a>";

preg_match_all($re, $str, $matches);
vks
  • 65,133
  • 10
  • 87
  • 119