开发者

Regular expression to extract link text from anchor tag

开发者 https://www.devze.com 2022-12-20 22:34 出处:网络
i want to get the word Asarum in the below line: <A HREF=开发者_JS百科\"http://www.getty.edu/vow/TGNFullDisplay?find=&place=&nation=&english=Y&subjectid=1062783\">Asarum</A&

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

 <A HREF=开发者_JS百科"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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号