So I think the function fgetcsv() will be usefull here. Say you have a file :
Code:
one two three
cat dog bird
man woman child
And you want to update "bird" (which is on the 3rd field column) and replace it
with "falcon"
PHP Code:
// your tab separated file
$file = "/path/to/file";
// a temp file
$tmp = tempnam("/tmp", "tmpfile");
// open tab separated file
$fd = fopen($file, "r");
if(!$fd) {
echo "Could not open $file in read only mode\n";
exit(1);
}
// open tmp file to write in
$fw = fopen($tmp, "w");
if(!$fw) {
echo "Could not open the temp file in write mode\n";
exit(1);
}
while (($line = fgetcsv($fd, 1000, "\t")) !== FALSE) {
if($line[2] == "bird") { $line[2] = "falcon"; }
$text = implode("\t", $line)."\n";
fwrite($fw, $text);
}
fclose($fd);
fclose($fw);
// now just copy temp file to tab separated file (need write permission)
copy($tmp, $file) or die("Could not copy tmp to $file");
unlink($tmp);