1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| function MoveLine($oldPosition, $newPosition, $fileName, $newFileName = false)
{
if(is_file($fileName)) $arr = file($fileName); else return "Missing filename: ".$fileName." !";
if(($oldPosition<1) || ($oldPosition>count($arr)) || ($newPosition<1) || ($newPosition>count($arr)))
{
return "Param1 or Param2 out of range !";
}
--$oldPosition;--$newPosition;
if(!$newFileName) $newFileName = $fileName;
$value2move = array_splice($arr, $oldPosition, 1);
array_splice($arr, $newPosition, 0, $value2move);
foreach($arr as $key => $value)
{
//if it does not end on n - make it end on n
if ($value[strlen($value)-1] != "n") $arr[$key] .= "n";
}
to 'optimize' this
$fp = fopen($newFileName, "w");
if(!$fp) return "Unable to write the result to file: ".$newFileName." !";
fwrite($fp, implode("",$arr));
fclose($fp);
return "Operation completed successful !";
}
echo MoveLine(4,1, "lines.txt", "output.txt");
?> |