Regular expression to extract link text from anchor tag

前端 未结 3 1365
不思量自难忘°
不思量自难忘° 2020-12-20 01:15

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

 

        
相关标签:
3条回答
  • 2020-12-20 01:43

    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 讨论(0)
  • 2020-12-20 01:50

    You need to assign the result:

    $link = preg_replace('/<a.*?>/i', '', $link);
    $link = preg_replace('/<\/a>/i', '', $link);
    echo $link;
    
    0 讨论(0)
  • 2020-12-20 01:50

    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);
    
    0 讨论(0)
提交回复
热议问题