Parse feed and write to file.

Discussion in 'PHP' started by nielsen, Feb 18, 2009.

  1. #1
    Hello all... I've something of a problem. For the life of me, I've been horribly unable to parse a twitter feed into something *writable to a text file.* There is tons of code out there to fetch the feed, replace the funky symbols etc etc, but I cannot figure out how to turn the screen display into a writable-to-text using fwrite() array or whatever.

    Essentially, I can't figure out how to turn:

    <a href="http://twitter.com/">web</a>   sicsemperhomo (Theodore)  http://twitter.com/sicsemperhomo     tag:search.twitter.com,2005:1221756053  2009-02-18T04:50:07Z   He had it coming.  2009-02-18T04:50:07Z   <a href="http://twitter.com/">web</a>   sicsemperhomo (Theodore)  http://twitter.com/sicsemperhomo     tag:search.twitter.com,2005:1221753403  2009-02-18T04:49:03Z   <a href="http://twitter.com/emilyc808">@emilyc808</a> So, dah-ling, I hear that you didn't recognize me in my picture behind a glass door with a flash obscuring my face.  2009-02-18T04:49:03Z   <a href="http://twitter.com/">web</a>   sicsemperhomo (Theodore)  http://twitter.com/sicsemperhomo     tag:search.twitter.com,2005:1214279357  2009-02-16T04:16:54Z   I wonder why _Brothers &amp; Sisters_ starting using the _Angels in America_ oboe motif.  ......... etc etc
    Code (markup):
    INTO

    # He had it coming.
    # @emilyc808 So, dah-ling, I hear that you didn't recognize me in my picture behind a glass door with a flash obscuring my face.
    # I wonder why _Brothers & Sisters_ starting using the _Angels in America_ oboe motif.
    # I wish I could find someone who knows twitter api to help me out with a project... any takers?
    # Blue sunlight is the product of IKEA curtains and mimosas. Woof.

    And write it to a text file.

    Thanks so much. It's driving me absolutely crazy. Here's the code I'm working with, and it's writing the WHOLE xml garbage into the file:
    
    <?php 
    
    // Define username and how much to pull; cleanup output with li
    
    $username = "sicsemperhomo"; 
    $limit = "15"; 
    $prefix = "<ul>";
    $suffix = "</ul>";
    $pretweet = "<li>";
    $posttweet = "</li>";
    
    $feed = "http://search.twitter.com/search.atom?q=from:" . $username . " . $limit . ";
    
    function parse_feed($feed, $prefix, $pretweet, $posttweet, $suffix) {
    
    //Beautify.
    
    $feed = str_replace("&lt;", "<", $feed);
    $feed = str_replace("&gt;", ">", $feed);
    $feed = str_replace("&amp;", "&", $feed);
    $feed = str_replace("&quot;", "\"", $feed);
    
    //Create a clean version of the code in html and overwrite the $feed string telling it to use cont... as the dynamite.
    $clean = explode("<content type=\"html\">", $feed);
    
    //Establish the number of times the php should loop by counting the number of clean lines minus 1.
    $amount = count($clean) - 1;
    
    echo $prefix;
    
    //Looptastic magic
    
    for ($i = 1; $i <= $amount; $i++) {
    
    
    $cleaner = explode("</content>", $clean[$i]);
    echo $pretweet;
    echo $cleaner[0];
    echo $posttweet;
    
    }
    
    echo $suffix;
    
    }
    
    $twitterFeed = file_get_contents($feed);
    echo $twitterFeed;
    
    parse_feed($twitterFeed, $prefix, $pretweet, $posttweet, $suffix);
    
    $codex = "RecentTwitters.txt";
     
    $codexhandle = fopen($codex, 'a') or die("Cannot perform up to standard. I need the attention of a webcoder.");
    
    fwrite($codexhandle, $twitterFeed);
    
    fclose($codexhandle);
    
    echo "<br /> <br />D.V. 'Tis done."; ?>
    Code (markup):
    Danke.
     
    nielsen, Feb 18, 2009 IP
  2. mallorcahp

    mallorcahp Peon

    Messages:
    141
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    mallorcahp, Feb 19, 2009 IP