My problem here is that i don't know how to properly write a file using fwrite
. I parse xml using simplexml and then, while echo
works fine, i can't write the same result in a file. In this example below, i echo 5 items from an xml feed, but when i write it to a file, it writes the 5th item only. Can anyone tell what's the problem?
$file = simplexml_load_file($url); {
$c = 0;
foreach($file->channel->item as $post) {
if($c == 5){
break;
}
$headline = (string) $post->title;
echo $headline;
$fp=fopen('myfile.htm', "w+");
fwrite($fp, $headline."<br />");
fclose($fp);
$c++;
}
}
EDIT : Second attempt . I used a while
loop, instead of if
, but i only managed to write 5 times the 5th item. I need w+
(and not a+
) to overwrite file every time it is updated and not add content each time:
$file = simplexml_load_file($url); {
foreach($file->channel->item as $post) {
$fp=fopen('myfile.htm', "w+");
$c = 1;
while ($c <= 5){
$headline = (string) $post->title;
fwrite($fp, "$headline<br />");
$c++;
}
fclose($fp);
}
include 'myfile.htm';
}