Removing All Links From Text With Anchor Text

Discussion in 'PHP' started by SNaRe, Mar 15, 2008.

  1. #1
    I have a text it has <a href="http://www.com">Anchor</a/>
    When i make it strip tags it gives "Anchor"
    But i want to filter this too so it will remove full of this
    <a href="http://www.com">Anchor</a/>
    from text
    I tried some codec preg_replace etc. but i couldn't do it . How can i do it ?
     
    SNaRe, Mar 15, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    $text = preg_replace('~<a[^>]+>(.*?)</a>~si', null, $text);
    
    PHP:
     
    nico_swd, Mar 15, 2008 IP
  3. SNaRe

    SNaRe Well-Known Member

    Messages:
    1,132
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    165
    #3
    It works gr8 thank you
     
    SNaRe, Mar 15, 2008 IP
  4. jackieshine

    jackieshine Banned

    Messages:
    242
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    if i need to keep the link text: Anchor
    what to do?
     
    jackieshine, Mar 18, 2008 IP
  5. greenbarey

    greenbarey Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    $text = preg_replace('~<a[^>]+>(.*?)>~si', null, $text);
    $text = preg_replace('</a>', null, $text);

    Try it
     
    greenbarey, Mar 18, 2008 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    ^ That's ugly... Try this instead:
    
    $text = preg_replace('~<a[^>]+>(.*?)</a>~si', '$1', $text);
    
    PHP:
     
    nico_swd, Mar 19, 2008 IP