-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
  • 42,425
  • 27
  • 92
  • 132

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
  • 4,035
  • 5
  • 27
  • 35
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,145
  • 1
  • 22
  • 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
  • 67,027
  • 10
  • 91
  • 124