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
  • 149
  • 3
  • 7

2 Answers2

-1

Looks funny to see the code you created. Btw, I explained through the comments.

Assumption :

yourfile.php?coords=22.300859182388606 -127.66133104264736

// Assumption : 
// 22.300859182388606 -127.66133104264736
$msg = isset($_GET['coords']) ? $_GET['coords'] : null;

if ($msg) {
    $file = 'coordinates.txt';
    // Change newline into array
    $items = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $new   = [];

    foreach ($items as $key => $item) {
        // Remove time
        // Yep, I see you're using unixtime (10 characters + 1 space)
        $check = substr($item, 0, -11);

        // If $msg === $check, append to $new
        if (strpos($check, $msg) === false) {
            $new[] = $item;
        }
    }

    // If $new has value
    if ($new) {
        // Write file with $new content
        file_put_contents($file, implode("\n", $new));
    }
}
Wahyu Kristianto
  • 7,521
  • 5
  • 39
  • 64
  • Your code would be far more efficient if you had looped the result of strrpos. The way you do it now you have to look at every single line. And why do you remove the time? Unnecessary and takes time. – Andreas Jun 10 '18 at 05:38
  • Yep, it because faster than `preg_match()`. Here you find the average performance. Your code https://3v4l.org/aW14j/perf#output and my code https://3v4l.org/rGe96/perf#output. Imagine you have thousands of data – Wahyu Kristianto Jun 10 '18 at 07:17
  • There is no need to imagine. A simple test will do. Also 3v4l's times are not to be trusted use microtime instead. Your code was not correct in two ways, you had an array of the items which OP does not and you var_dumped the array you didn't make it string. With apples compared to apples and a larger sample, as you said yourself it is quite noticeable performance, I agree... https://3v4l.org/NHcYk – Andreas Jun 10 '18 at 07:33
  • I'm not using `file_get_contents()`. I use `file()` and will return an array :) – Wahyu Kristianto Jun 10 '18 at 07:51
  • Not by magic. How do you think file() creates the array? – Andreas Jun 10 '18 at 08:02
  • It's not magic :) http://php.net/manual/en/function.file.php – Wahyu Kristianto Jun 10 '18 at 08:07
  • I know where the manual is no need to link to it. Question is still how do you think file() makes it an array? A text file is not an array it's a string. Somehow file makes the string an array. We agree it's not magic so what other possibilities are there? – Andreas Jun 10 '18 at 08:15
  • Did you try it? – Wahyu Kristianto Jun 10 '18 at 10:14
-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,304
  • 5
  • 28
  • 61