简体   繁体   English

正则表达式从锚标记中提取链接文本

[英]Regular expression to extract link text from anchor tag

i want to get the word Asarum in the below line: 我想在下一行得到Asarum一词:

 <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. $matches[1]现在将包含您要查找的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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