You have to read the whole file to a variable, replace one section of it, then write it all out again.
Let's assume the contents of the file is:
1||first||2||second||3||third
Your code would be:
$file = file_get_contents ('./file.txt');
$items = explode ('||', $file);
for ($i = 0; $i < count ($items) / 2; $i++) {
$sets[] = array ($items[2 * $i], $items[2 * $i + 1]);
}
/* the array now looks like:
$sets = array (array ('1', 'first'), array ('2', 'second'), array ('3', 'third'));
*/
// to delete the item, set $new_string = false;
// to edit, set it to something else
$key_to_replace = '2';
$new_string = 'hey there';
just copy and paste this new foreach loop:
foreach ($sets as $k => $v) {
if ($v[0] == $key_to_replace) {
if ($new_string === false) {
unset ($sets[$k]);
} else {
$sets[$k][1] = $new_string;
}
break;
}
}
$edit = '';
foreach ($sets as $v) {
$edit .= $v[0] . '||' . $v[1] . '||';
}
$fopen = fopen ('./file.txt', 'w');
fwrite ($fopen, substr ($edit, 0, strlen ($edit) - 2));
fclose ($fopen);
I hope that's got it. 😃 I'll test and edit if I need to, haha.