Hi,
I tried to update the flat file using php by renaming the existing file and insert it back after doing some comparison. However, my problem is I'm not sure why there are additioanl newline inserted when I only specify once only. Could anyone please help me?
Below are the codes.
if (rename("data.txt", "tempdata.txt"))
{
$pNo = $txtNo;
$pKeyword = $txtPrevKeyword;
$nKeyword = $txtKeyword;
$fp=fopen("data.txt", "a");
if (!$fp) {
echo "error!, The file could not be opened!";
exit;
}
$array = file("tempdata.txt");
foreach($array as $row) {
$line = explode("|", $row);
if (TRIM($pNo) <> "" AND TRIM($line[0]) <> "")
{
if (TRIM($line[0]) == TRIM($pNo) && TRIM(preg_replace("[%20]"," ",$line[1]))==TRIM(preg_replace("[%20]"," ",$pKeyword)))
{
$stringToInsert = $pNo."|".$nKeyword."\n";
fwrite($fp, $stringToInsert);
}
else
{
$stringToInsert = $line[0]."|".preg_replace("[%20]"," ",$line[1]);
fwrite($fp, $stringToInsert);
}
}
}
fclose;
header("Location:KeywordListing.php");
unlink("tempdata.txt");
}
else
{
echo "Updating fail!";
header("Location:KeywordListing.php");
}