Stripping HTML from RSS feed

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

First things first, let me say that I couldn't decide whether to post
this to the PHP ng, or to an XML ng. I know from experience that you
guys know what you're talking about, though, and all of the questions
mean "how to do this in PHP," so I hope I picked the right one ;-)

For about a year, I've been importing Yahoo News headlines into my site
via their RSS feed. But I would much rather import Google News
headlines because I can make them specific to my location. The problem
is that their RSS feed includes HTML, and the script I use can't figure
it out.

Here's an example tag from their most recent feed:

<description><br><table border=0 width= valign=top cellpadding=2
cellspacing=7><tr><td valign=top><a
href=" ">Truck
Driver Wins $100,000 In Lottery; <b>NC</b> Powerball
Winners</a><br><font size=-1><font color=#6f6f6f>Raleigh
Chronicle,&nbsp;NC&nbsp;-</font> <nobr>2 hours
ago</nobr></font><br><font size=-1>REIDSVILLE -- According to the
<b>NC</b> Lottery Commission, when Dennis Mebane collected his prize
this week from a winning $100,000 instant scratch-off ticket, he
<b>...</b> </font><br></table></description>

The XML tag is <description>, but the script I use to parse it
(rss2array, a cookie-cutter script that I downloaded) can't
differentiate between <description> and <table>. Usually, I would call
<description> by using the variable $rss['items'][$i]['description']
(where $i is the index counter), but with this output I would have to
do something like

So I guess I really have 2 questions:

1. Is there a way to strip HTML completely out of the tag? Or better
yet, to make rss2array read the HTML as actual HTML instead of XML

2. If not, is there a better way to read the XML than through the use
of rss2array? I'm a fairly experienced coder, but don't really use XML
often enough to have a good grasp of the logic.



Re: Stripping HTML from RSS feed

See if this works:

1. Use $rss['items'][$i]['description'] to return the contents of the
description node (including html markup) as a string.
2. Use the 'strip_tags' function to remove unwanted HTML from the
returned string.

See for more info.

Jason wrote:
Quoted text here. Click to load it
href=" ">Truck
Quoted text here. Click to load it

Re: Stripping HTML from RSS feed

I'm afraid that didn't work. It reads the variable as empty.

As a test, I tried print_r($rss), and while it reads everything else
correctly, it shows ['description'] as an empty variable. The only ones
that it reads correctly are the ones that don't have HTML.

Knowing this, it's got to be a "problem" with rss2array. I put
"problem" in quotes, because technically I think it's the XML file
that's flawed, but there's not much I can do about that. What other way
can I parse an XML database using PHP?

- Jason

Quoted text here. Click to load it

Re: Stripping HTML from RSS feed

There are two things I can suggest:

1. Using the DOM XML functions to extract the values (See ), then use the strip_tags() functions.
2. Use Regular expressions to extract the html tags from the XML before
you use rss2array


// Remove all <p> tags
$xml = preg_replace('/<p(.*)?>(.*)?<\/p>/', "$2", $xml);

//Remove all <font> tags
$xml = preg_replace('/<font(.*)?>(.*)?<\/font>/', "$2", $xml);


Jason wrote:
Quoted text here. Click to load it

Site Timeline