Write exploded string to file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nielsen
Forum Newbie
Posts: 1
Joined: Wed Feb 18, 2009 12:09 am

Write exploded string to file

Post 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(); ?>
User avatar
Attero
Forum Newbie
Posts: 18
Joined: Thu Jan 29, 2009 9:35 am
Location: Farnborough, UK

Re: Write exploded string to file

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Write exploded string to file

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply