ajsie
ajsie

Reputation: 79676

Regular expression to extract link text from anchor tag

i want to get the word Asarum in the below line:

 <A HREF="http://www.getty.edu/vow/TGNFullDisplay?find=&place=&nation=&english=Y&subjectid=1062783">Asarum</A>

i tried with:

preg_replace('/<a.*?>/i', '', $link);
preg_replace('/<\/a>/i', '', $link);
echo $link;

but it doesn't work. nothing was cut out.

could someone help me?

Upvotes: 2

Views: 2921

Answers (3)

Steve Clay
Steve Clay

Reputation: 8781

Fastest (probably - not tested):

$t = substr($link, strpos($link, '>') + 1, -4);

Clearest:

$t = strip_tags($link);

Basic strip tags w/ regex:

$t = preg_replace('/<[^>]*>/', '', $link);

Upvotes: 4

Douwe Maan
Douwe Maan

Reputation: 6878

Use this code, to match the text to a specific regex, instead of replacing everything else:

preg_match('/<a.*?>(.+?)<\/a>/i', $link, $matches);

$matches[1] will now contain the text you're looking for.

Upvotes: 2

kennytm
kennytm

Reputation: 523214

You need to assign the result:

$link = preg_replace('/<a.*?>/i', '', $link);
$link = preg_replace('/<\/a>/i', '', $link);
echo $link;

Upvotes: 2

Related Questions