简体   繁体   中英

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?

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);

You need to assign the result:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM