Forum Moderators: coopster
Use file [uk3.php.net] to read the text file into an array, then loop through the data. When you find a match, make the change, then clear the original text file and write the new data. As you have a hyphen (-), you can use that as a delimiter to explode the data on the loop.
So, something like this:
$replace_id = '03';
$replace_string = 'Fred';
$write_string = '';
$file = file('file.txt');for ($i=0; $i<count($file); $i++)
{
$line = explode("-",$file[$i]);if ($line[0]==$replace_id)
{
$write_string = $line[0].'-'.$replace_string."\n";
}
else
{
$write_string .= $line[0].'-'.$line[1]."\n";
}}
Then clear the original file and write the new one:
unlink('file.txt');
$fp = fopen('file.txt', 'ab');if ($fp)
{
fwrite($fp,trim($write_string));
fclose($fp);
}
Hope that helps.
dc