Page 1 of 1

Write exploded string to file

Posted: Wed Feb 18, 2009 12:10 am
by nielsen
Hello folks,

One of the bloggers on my website recently started twitter-ing, and he's hysterical, so I'd like to figure out a way to post his twitter to a page on the website. Small problem: not all of his tweets are necessarily appropriate for the medium, so I'm trying to write code to generate a .txt file with his most recent 15 tweets for someone to sort through and post as would be appropriate.

I've borrowed and edited some code to pull the twitter via rss, and it works like a gem up until comment //FIDDLESTICKS!!

As far as I understand it, being a newbie, that the explode function creates an array... I'd like to figure out a way to write it to a file when the script is run.

Thanks SO much for your help!

Cheers!

Code:

Code: Select all

<?php
/*
Template Name: Twitter
*/
?>
 
<?php get_header(); ?>
 
<?php 
 
// Define username and how much to pull; cleanup output with listing
 
$username = "TWITTERUSER"; 
$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("<", "<", $feed);
$feed = str_replace(">", ">", $feed);
$feed = str_replace("&", "&", $feed);
$feed = str_replace(""", "\"", $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
$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);
//This is the part that seems to print out the results; it can't be saved as a string, which I tried in the next line...
parse_feed($twitterFeed, $prefix, $pretweet, $posttweet, $suffix);
 
/* FAILED: echo $filecontents or die("DARN!!"); */
 
// FIDDLESTICKS!!
 
/* $codex = "RecentTwitters.txt";
 
$codexhandle = fopen($codex, 'a') or die("Cannot perform up to standard. I need the attention of a webcoder.");
 
fwrite($codexhandle, $filecontents);
 
fclose($codexhandle);
 
echo "<br /> <br />D.V. 'Tis done."; */ ?>
 
<?php get_footer(); ?>

Re: Write exploded string to file

Posted: Wed Feb 18, 2009 4:01 am
by Attero
Just to let you know, file_get_contents() is only for PHP >= 5.

It would be much better (but much more complex) to use fsockopen() instead. Try the different methods. fsockopen(), and cURL functions.

Re: Write exploded string to file

Posted: Wed Feb 18, 2009 10:21 am
by pickle
If you're running your own website & your host has PHP5 - stick with file_get_contents() - much easier.

You can't really store an array in it's native format in a file - because it's an array, not a string. You can serialize() the array though, before writing to file, then unserialize() after read.