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> ( <strong>Release:</strong> May. 30, 2008 <strong>Rated:</strong> R - for strong sexual content, graphic nudity and language <strong>Avg. Score:</strong> 3.21/5 <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>   — 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!
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:
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.
You're welcome. If you're going to be doing this sort of pattern matching or replacing, you might be interested in learning "regular expressions". Here are the links to a few good sites with tutorials : http://www.regular-expressions.info http://www.roscripts.com/PHP_regular_expressions_examples-136.html The second article has a lot of useful script examples such as matching credit card numbers, ip addresses, email addresses, etc...