Code I tested is
Code: Select all
$snoopy = new Snoopy;
$snoopy->fetchtext($url);
$resulttext= $snoopy->results;
$myFile = "testfile";
echo "file opened";
$fh = fopen($myFile, 'a') or die("can't open file");
// needed otherwise filesize() may return wrong size
clearstatcache();
// calc how many bytes we can write
$remaining_bytes = 5000 - filesize($myFile);
// truncate our data if its too big
if (strlen($resulttext) > $remaining_bytes) {
$resulttext = substr($resulttext, 0, $remaining_bytes);
}
//fwrite($fh, $resulttext);
//////////////////////////
//search and replace
$patterns[0] = '/"/';
$patterns[1] = '/Comments/';
$patterns[2] = '/Have something you would like to share?/';
$replacements[2] = '<p>';
$replacements[1] = '';
$replacements[0] = '';
$replaced=preg_replace($patterns, $replacements, $resulttext);
//search and replace
$stringData = $replaced;
fwrite($fh, $stringData);
$stringData = "\n";
fwrite($fh, $stringData);
fclose($fh);
//echo $replaced;
////////////////
// return the PART_IX of the last file used
function write_feed($file_name, $content, $part_ix = 0, $max_length = 2000)
{
clearstatcache();
$finished = false;
while (!$finished)
{
$full_file_name = $file_name.($part_ix == 0 ? '.txt' : '.'.$part_ix.'.txt');
$handle = @fopen($full_file_name, 'a+') or die("can't open file ".$full_file_name);
$remaining_bytes = $max_length - filesize($full_file_name);
if (strlen($content) > $remaining_bytes)
{
$partial_content = substr($content, 0, $remaining_bytes);
$content = substr($content, $remaining_bytes + 1);
$part_ix ++;
}
else
$finished = true;
fwrite($handle, $content);
fclose($handle);
}
return $part_ix;
}
$last=write_feed($myFile,$replaced,0,4000);
echo $last;
I tested this
1> The first file has contents of 4 kb ...ok
2> the second file has the contents which are repeated from the contents of 1st file
3>the third file also has the contents which are repeated from the contents of 1st file.
means 2nd and 3rd file have duplicate contents which are indeed repeated contents from 1st fileHello again,