0

I have a text file that contains a bunch lines of text, formatted as

latitude longitude time

22.300859182388606 -127.66133104264736 1528577039
22.30103320995603 -127.66234927624464 1528577041
22.300184137952726 -127.661628767848 1528577042
22.29943548054545 -127.66242001950741 1528577045

I am given coordinates and I want to search the text file for the same coordinates, and if there are, delete the line from the file. How do I search for the same coordinates as the given coordinates and delete it from the file? This is the code that I have so far:

<?php
$msg = $_GET["coords"];
$file = 'coordinates.txt';
// Open the file to get existing content
$current = file_get_contents($file);

?>
Arnold Hotz
  • 179
  • 1
  • 3
  • 8

1 Answers1

-1

A simple way to do it is to use preg_replace that can search for the coordinates and "wildcard" (.*) and new line (\n).

$txt = file_get_contents("coordinates.txt");

$find = $_GET["coords"];

Echo preg_replace("/". $find . ".*\n/", "", $txt);

See it in action here:
https://3v4l.org/aW14j

This requires the user to input the coordinates in correct order and with space separated.
Usually it is comma space separated.
You can fix comma space with:

$find = str_replace(", ", " ", $_GET["coords"]);

If the user types in the wrong order is possible to fix, but it can also delete lines you want to keep.
If you want this just tell me and I will add that code.

Andreas
  • 23,610
  • 6
  • 30
  • 62