By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,156 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

rss escape

P: n/a
One thing I admire about php is that there is usually a function that
does exactly what you want.

I'm building an RSS feed (something I had in perl).

There's a lot about escaping rss data I don't really understand, I've
been doing this:

'&','&'
'\n','<br \/>'
'',"'"
'<','&lt;'
'>','&gt;'
'',''

Is there something in php that can more directly or simply help me
build rss?

Jeff
Sep 21 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
..oO(Jeff)
One thing I admire about php is that there is usually a function that
does exactly what you want.

I'm building an RSS feed (something I had in perl).

There's a lot about escaping rss data I don't really understand, I've
been doing this:

'&','&amp;'
'\n','<br \/>'
'’',"'"
'<','&lt;'
'>','&gt;'
'™',''
For &, < and you can use htmlspecialchars(), for \n use nl2br() if
necessary. The rest shouldn't matter.
Is there something in php that can more directly or simply help me
build rss?
I build my feeds with the DOM extension.

Micha
Sep 21 '08 #2

P: n/a
Michael Fesser wrote:
.oO(Jeff)
> One thing I admire about php is that there is usually a function that
does exactly what you want.

I'm building an RSS feed (something I had in perl).

There's a lot about escaping rss data I don't really understand, I've
been doing this:

'&','&amp;'
'\n','<br \/>'
'’',"'"
'<','&lt;'
'>','&gt;'
'™',''

For &, < and you can use htmlspecialchars(), for \n use nl2br() if
necessary. The rest shouldn't matter.
As always, thanks!
>
> Is there something in php that can more directly or simply help me
build rss?

I build my feeds with the DOM extension.
I've just looked through this. Are you using that to generate the RSS or
to read the html. Care to share any details? I can't see how this would
write an RSS feed, but I may have missed something.

Jeff
>
Micha
Sep 23 '08 #3

P: n/a
..oO(Jeff)
>Michael Fesser wrote:
>I build my feeds with the DOM extension.

I've just looked through this. Are you using that to generate the RSS or
to read the html. Care to share any details? I can't see how this would
write an RSS feed, but I may have missed something.
I use it to create the RSS. My feed is just another script, which
collects the data for the feed and then uses DOM to create the XML for
the feed.

Somewhere in my RSS component I have the following method (slightly
modified here, because the actual data may come from different sources
like a database for example):

private function createXml() {
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->formatOutput = TRUE;
$rss = $doc->createElement('rss');
$rss->appendChild(new DOMAttr('version', '2.0'));
$doc->appendChild($rss);
$channel = $doc->createElement('channel');
$rss->appendChild($channel);
// create channel info like title, description and URL
foreach (getChannelInfoFromSomewhere() as $field =$value) {
$channel->appendChild(new DOMElement($field,
htmlspecialchars($value))
);
}
// create items and append to channel
foreach (getItemDataFromSomewhere() as $data) {
$item = $doc->createElement('item');
foreach ($data as $field =$value) {
$item->appendChild(new DOMElement($field,
htmlspecialchars($value))
);
}
$channel->appendChild($item);
}
return $doc;
}

This method creates the entire feed and returns it as a DOM document.
The calling method then more or less just has to output the XML to the
browser (besides setting correct HTTP response headers):

$xml = $this->createXml()->saveXML();
...
print $xml;

One result of this all you can see here:

http://ra-pauli.de/beratung/verkehrsrecht/rss

Have a look at the source code - this is what is created by my
createXml() method and the DOM::saveXML() output method.

Micha
Sep 23 '08 #4

P: n/a
Michael Fesser wrote:
.oO(Jeff)
>Michael Fesser wrote:
>>I build my feeds with the DOM extension.
I've just looked through this. Are you using that to generate the RSS or
to read the html. Care to share any details? I can't see how this would
write an RSS feed, but I may have missed something.

I use it to create the RSS. My feed is just another script, which
collects the data for the feed and then uses DOM to create the XML for
the feed.

Somewhere in my RSS component I have the following method (slightly
modified here, because the actual data may come from different sources
like a database for example):
Thanks Micha, I hadn't found that yet.

Generally I've been assembling html using templates. But there is a
certain beauty in this method and I'm sure you never have to worry about
whether your XML is well formed (like missing ending tags). I'll work my
way through this later.
>
private function createXml() {
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->formatOutput = TRUE;
$rss = $doc->createElement('rss');
$rss->appendChild(new DOMAttr('version', '2.0'));
$doc->appendChild($rss);
$channel = $doc->createElement('channel');
$rss->appendChild($channel);
// create channel info like title, description and URL
foreach (getChannelInfoFromSomewhere() as $field =$value) {
$channel->appendChild(new DOMElement($field,
htmlspecialchars($value))
);
}
// create items and append to channel
foreach (getItemDataFromSomewhere() as $data) {
$item = $doc->createElement('item');
foreach ($data as $field =$value) {
$item->appendChild(new DOMElement($field,
htmlspecialchars($value))
);
}
$channel->appendChild($item);
}
return $doc;
}

This method creates the entire feed and returns it as a DOM document.
The calling method then more or less just has to output the XML to the
browser (besides setting correct HTTP response headers):

$xml = $this->createXml()->saveXML();
...
print $xml;

One result of this all you can see here:

http://ra-pauli.de/beratung/verkehrsrecht/rss

Have a look at the source code - this is what is created by my
createXml() method and the DOM::saveXML() output method.
Hmm, not at all what I expected! I'll be thinking about this for a
while, but not this morning...

Regards,
Jeff
>
Micha
Sep 24 '08 #5

P: n/a
..oO(Jeff)
>Michael Fesser wrote:
>.oO(Jeff)
>>Michael Fesser wrote:

I build my feeds with the DOM extension.
I've just looked through this. Are you using that to generate the RSS or
to read the html. Care to share any details? I can't see how this would
write an RSS feed, but I may have missed something.

I use it to create the RSS. My feed is just another script, which
collects the data for the feed and then uses DOM to create the XML for
the feed.

Somewhere in my RSS component I have the following method (slightly
modified here, because the actual data may come from different sources
like a database for example):

Thanks Micha, I hadn't found that yet.
You're welcome.
Generally I've been assembling html using templates.
Me too, but only for "normal" pages, not for RSS or the like.
>But there is a
certain beauty in this method and I'm sure you never have to worry about
whether your XML is well formed (like missing ending tags).
Exactly. With DOM I just create the structure of objects, which can then
easily be exported into well-formed markup.

Micha
Sep 24 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.