Help removing <a> tags from a string (PHP)

Discussion in 'PHP' started by Abuntis, Jun 7, 2008.

  1. #1
    Hi. I hope somebody can help me solve this problem. I have a string ($description) that comes from a movie rss feed, and I would like to remove the links from it, but keep the image and text. An example of the string is:

    <a href="http://movies.go.com/sex-and-the-city-the-movie/d917890/comedy&CMP=OTC-RY3575180518"><img src="http://movies.go.com/i/movies/917890/917890.jpg" align="right" width="200" height="125" border="1" alt="Sex and the City"/></a>
    (&nbsp;<strong>Release:</strong> May. 30, 2008&nbsp;&nbsp;&nbsp;<strong>Rated:</strong> R - for strong sexual content, graphic nudity and language&nbsp;&nbsp;&nbsp;<strong>Avg. Score:</strong> 3.21/5 &nbsp;&nbsp;&nbsp;<strong>

    <a href="http://movies.go.com/sex-and-the-city-the-movie/d917890/comedy&CMP=OTC-RY3575180518">Details</a> | <a href="http://movietimes.go.com/movietimes/movie?id=60729&CMP=OTC-RY3575180518">Tickets</a> | <a href="http://movies.go.com/sex-and-the-city-the-movie/m917890/comedy&CMP=OTC-RY3575180518">Trailers</a> | <a href="http://movies.go.com/sex-and-the-city-the-movie/m917890/comedy&CMP=OTC-RY3575180518">Photos</a> | <a href="http://movies.go.com/sex-and-the-city-the-movie/r917890/comedy&CMP=OTC-RY3575180518">Reviews</a></strong>
    &nbsp;) —
    Picking up where the TV series left off four years ago, Carrie Bradshaw (Sarah Jessica Parker) is on the verge of marrying the love of her life, Mr. Big (Chris Noth). Miranda Hobbes (Cynthia Nixon) must deal with some shocking news from her seemingly devoted husband, Steve (David Eigenberg). Charlotte York Goldenblatt (Kristen Davis) is going to have a baby. Finally, Samantha Jones (Kim Cattrall) is now living in a different city: Los Angeles.

    <br><bold>


    Any help would be gratefully received. thanks!
     
    Abuntis, Jun 7, 2008 IP
  2. Oli3L

    Oli3L Active Member

    Messages:
    207
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Try this:
    
    <?php
    function strip_selected_tags($text, $tags = array())
    {
        $args = func_get_args();
        $text = array_shift($args);
        $tags = func_num_args() > 2 ? array_diff($args,array($text))  : (array)$tags;
        foreach ($tags as $tag){
            while(preg_match('/<'.$tag.'(|\W[^>]*)>(.*)<\/'. $tag .'>/iusU', $text, $found)){
                $text = str_replace($found[0],$found[2],$text);
            }
        }
    
        return preg_replace('/(<('.join('|',$tags).')(|\W.*)\/>)/iusU', '', $text);
    }
    $var = "text text text";
    
    echo strip_selected_tags($var,array('a'));
    ?>
    
    
    PHP:
     
    Oli3L, Jun 7, 2008 IP
  3. Bryce

    Bryce Peon

    Messages:
    1,235
    Likes Received:
    93
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you want to do it quick and dirty without a function...

    $new_string = preg_replace('/<a.*?>/', '', $description);
    $new_string = preg_replace('/<\/a>/', '', $new_string );

    $new_string will be the data with the <a> tags removed.
     
    Bryce, Jun 7, 2008 IP
  4. Abuntis

    Abuntis Peon

    Messages:
    127
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks a lot, those are both really useful solutions.
     
    Abuntis, Jun 7, 2008 IP
  5. Bryce

    Bryce Peon

    Messages:
    1,235
    Likes Received:
    93
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Bryce, Jun 7, 2008 IP