www.webdeveloper.com
Results 1 to 2 of 2

Thread: Parse feed.xml to feed.php on the fly

  1. #1
    Join Date
    Jul 2007
    Posts
    7

    Parse feed.xml to feed.php on the fly

    I'm trying to add a news ticker on a website.
    The data headlines are based on a RSS feed.
    The ticker needs to read a php file of the feed.xml RSS file data.
    The RSS feed will change constantly, therefore the php file referenced in the ticker would work best if it updated automatically from the feed.xml file.

    I know next to nothing about php, but I assume this is possible. The feed.xml simply becomes a variable within the feed.php file, populates the ticker, and off it goes.

    The data in the feed.xml file is, for example

    <?xml version="1.0" ?>
    <rss version="2.0">
    <channel>
    <title>The Website Name</title>
    <description>The Website Description</description>
    <link>http://www.thewebsiteurl.com</link>

    <item>
    <title>Headline article</title>
    <description></description>
    <guid isPermaLink="true">http://www.thewebsiteurl.com/specificarticle.html</guid>
    <link>http://www.thewebsiteurl.com/specificarticle.html</link>
    </item>

    </channel>
    </rss>

    I want to grab the </ item> data, so that if you click on the headline <title> in the ticker it will open the <link> url in a new window. The php file needs to grab those two elements, and from there I can work on the javascript to make that happen. (ever the optimist)

    see also this thread http://www.webdeveloper.com/forum/sh...409#post776409

    thanks!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,918
    One way:
    PHP Code:
    <?php
    $rss 
    file_get_contents("http://domain.com/rss/feed.xml");
    $regex '#<item>.*<title>(.*)</title>.*<link>(.*)</link>.*</item>#Us';
    preg_match_all($regex$rss$matchesPREG_SET_ORDER);
    echo 
    "<ul>\n";
    foreach(
    $matches as $article)
    {
       
    printf("<li><a href='%s'>%s</a></p>\n"$article[2], $article[1]);
    }
    echo 
    "</ul>\n";
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles