Rss feed title styling

Discussion in 'PHP' started by fadetoblack22, Jul 18, 2008.

  1. #1
    I have the following code for rss output on my site.


    <?php
    /*
    
    Class RSSParser:    2 October 2002
    
    Author:    Duncan Gough
    
    Overview:         An RSS parser that uses PHP and freely available RSS feeds to add fresh news content to your site.
    
    Installation:      Decompress the file into your webroot and include it from whichever pages on which you want
                        to display the data, e.g;
    
                        include("rss.php");
    
    Usage:        As above, just include the rss.php file from within your PHP page, and the news will appear.
                        You should find the HTML code in the functions endElement(), show_title() and show_list_box() below,
                        feel free to modify these to match your site.
    */
    
    class RSSParser {
    
        var $title          = "";
        var $link           = "";
        var $description    = false;
        var $inside_item    = false;
        var $limit    = 0 ;
        var $done         = 0 ;
        
        function RSSParser( $limit = 0 )
        {
            $this->limit = $limit ;
        }
    
        function startElement( $parser, $name, $attrs='' ){
            global $current_tag;
    
            $current_tag = $name;
    
            if( $current_tag == "ITEM" )
                $this->inside_item = true;
    
        } // endfunc startElement
    
        function endElement( $parser, $tagName, $attrs='' ){
            global $current_tag;
    
            if ( $tagName == "ITEM" ) {
    
                printf( "\t<br><b><a href='%s' rel='nofollow'>%s</a></b>\n", trim( $this->link ), htmlspecialchars( trim( $this->title ) ) );
                printf( "\t<br>%s<br>\n", htmlspecialchars( trim( $this->description ) ) );
    
                $this->title = "";
                $this->description = "";
                $this->link = "";
                $this->inside_item = false;
                
                if( $this->limit and ( $this->limit > $this->done++ ) )
                {
                    $this->stop = true ;
                }
            }
    
        } // endfunc endElement
    
        function characterData( $parser, $data ){
            global $current_tag;
    
            if( $this->inside_item ){
                switch($current_tag){
    
                    case "TITLE":
                        $this->title .= $data;
                        break;
                    case "DESCRIPTION":
                        $this->description .= $data;
                        break;
                    case "LINK":
                        $this->link .= $data;
                        break;
    
                    default:
                        break;
    
                } // endswitch
    
            } // end if
    
        } // endfunc characterData
    
        function parse_results( $xml_parser, $rss_parser, $file )   {
    
            xml_set_object( $xml_parser, &$rss_parser );
            xml_set_element_handler( $xml_parser, "startElement", "endElement" );
            xml_set_character_data_handler( $xml_parser, "characterData" );
    
            $fp = fopen("$file","r") or die( "Error reading XML file, $file" );
    
            while ($data = fread($fp, 4096) and !$this->stop )    {
           
                // parse the data
                xml_parse( $xml_parser, $data, feof($fp) ) or die( sprintf( "XML error: %s at line %d", xml_error_string( xml_get_error_code($xml_parser) ), xml_get_current_line_number( $xml_parser ) ) );
               
            } // endwhile
    
            fclose($fp);
    
            xml_parser_free( $xml_parser );
    
        } // endfunc parse_results
    
    
    } // endclass RSSParser
    
    global $rss_url;
    
    // Set a default feed
    if( $rss_url == "" )
        $rss_url = "http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml";
    
    $xml_parser = xml_parser_create();
    $rss_parser = new RSSParser(4);
    
    $rss_parser->parse_results( $xml_parser, &$rss_parser, $rss_url );
    ?>
    Code (markup):
    I smiply need to know how to change the following bit to style the title of each article:

    printf( "\t<br><b><a href='%s' rel='nofollow'>%s</a></b>\n", trim( $this->link ), htmlspecialchars( trim( $this->title ) ) );
                printf( "\t<br>%s<br>\n", htmlspecialchars( trim( $this->description ) ) );
    Code (markup):
    <br><p><b><a href='%s' rel='nofollow'>%s</a></p></b>

    works, but if I put styling in the <p> an error shows instead of the feed.

    Can someone help please?
     
    fadetoblack22, Jul 18, 2008 IP
  2. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Either use an external style sheet, or escape any quotes you have in the styling eg " turns to \"
     
    matthewrobertbell, Jul 18, 2008 IP
    fadetoblack22 likes this.
  3. fadetoblack22

    fadetoblack22 Well-Known Member

    Messages:
    2,399
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    160
    #3
    thanks, thats great help :)
     
    fadetoblack22, Jul 19, 2008 IP