Using preg_replace to remove links from a string?

Discussion in 'PHP' started by Kerosene, Apr 10, 2009.

  1. #1
    How can I use preg_replace to remove the links from a string, but still keep the anchor text?

    <a href="blabla">I want to keep this text</a>
    <a target="blabla" href="blabla">I want to keep this text</a>
     
    Kerosene, Apr 10, 2009 IP
  2. emed

    emed Peon

    Messages:
    70
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    preg_replace("/\<a(.*)\>(.*)\<\/a\>/iU", "$2", $string);
    
    or
    
    preg_replace("/\<a([^>]*)\>([^<]*)\<\/a\>/i", "$2", $string);
    PHP:
    i think that should work
     
    emed, Apr 11, 2009 IP
    Kerosene likes this.
  3. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #3
    Yep. Very nice :) I worked it out, but your solution is far more elegant than the crap I was using.

    +REP :D
     
    Kerosene, Apr 11, 2009 IP