rss escape

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff

    rss escape

    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
  • Michael Fesser

    #2
    Re: rss escape

    ..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 htmlspecialchar s(), 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

    Comment

    • Jeff

      #3
      Re: rss escape

      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 htmlspecialchar s(), 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

      Comment

      • Michael Fesser

        #4
        Re: rss escape

        ..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(ne w DOMAttr('versio n', '2.0'));
        $doc->appendChild($r ss);
        $channel = $doc->createElement( 'channel');
        $rss->appendChild($c hannel);
        // create channel info like title, description and URL
        foreach (getChannelInfo FromSomewhere() as $field =$value) {
        $channel->appendChild(ne w DOMElement($fie ld,
        htmlspecialchar s($value))
        );
        }
        // create items and append to channel
        foreach (getItemDataFro mSomewhere() as $data) {
        $item = $doc->createElement( 'item');
        foreach ($data as $field =$value) {
        $item->appendChild(ne w DOMElement($fie ld,
        htmlspecialchar s($value))
        );
        }
        $channel->appendChild($i tem);
        }
        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:

        Unfallregulierung | 13156 Berlin |Pankow |Verkehrsanwalt |Bußgeld |Geschwindigkeitsüberschreitungen | Strafverfahren (z.B. wegen unerlaubten Entfernens vom Unfallort - sog. Fahrerflucht)


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

        Micha

        Comment

        • Jeff

          #5
          Re: rss escape

          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(ne w DOMAttr('versio n', '2.0'));
          $doc->appendChild($r ss);
          $channel = $doc->createElement( 'channel');
          $rss->appendChild($c hannel);
          // create channel info like title, description and URL
          foreach (getChannelInfo FromSomewhere() as $field =$value) {
          $channel->appendChild(ne w DOMElement($fie ld,
          htmlspecialchar s($value))
          );
          }
          // create items and append to channel
          foreach (getItemDataFro mSomewhere() as $data) {
          $item = $doc->createElement( 'item');
          foreach ($data as $field =$value) {
          $item->appendChild(ne w DOMElement($fie ld,
          htmlspecialchar s($value))
          );
          }
          $channel->appendChild($i tem);
          }
          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:
          >
          Unfallregulierung | 13156 Berlin |Pankow |Verkehrsanwalt |Bußgeld |Geschwindigkeitsüberschreitungen | Strafverfahren (z.B. wegen unerlaubten Entfernens vom Unfallort - sog. Fahrerflucht)

          >
          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

          Comment

          • Michael Fesser

            #6
            Re: rss escape

            ..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

            Comment

            Working...