Rip Hunter
Rip Hunter

Reputation: 97

Remove duplicate line with random starts php

I have a file containing data something like this:

randomString-data1
randomString-data2
randomString-data4
randomString-data2
randomString-data2
randomString-data3
randomString-data4

The output I want:

randomString-data1
randomString-data2
randomString-data4
randomString-data3

The length of random string is between 20 to 25 characters. The randomString is not important. The data after it is of relevance only. What could be the best way to remove such duplicates?

All I can think of is the brute force way to explode each line at "-" and compare with all others. But is there a better way?

Note: The random strings are important. But it is okay if any one of the randomString remains from the duplicate entries.

Upvotes: 0

Views: 54

Answers (3)

Oliver
Oliver

Reputation: 214

i would do it this way

<?php
$file = <<<EOF
randomString-data1
randomString-data2
randomString-data4
randomString-data2
randomString-data2
randomString-data3
randomString-data4
EOF;

$found = [];
foreach (explode("\n", $file) as $line) {
    list(, $date) = explode('-', $line);
    if (!isset($found[$date])) {
        echo "$line<br>";
        $found[$date] = true; 
    }
}
?>

Upvotes: 1

Hardeep Singh
Hardeep Singh

Reputation: 780

firstly explode them by '\n' then by '-'

$arr = explode('\n', $str);

$data = array();
foreach($arr as $val)
{
   $temp = explode("-", $val);
   $data[$temp[1]] = $temp[0];
}

$str = "";
foreach($data as $k => $v)
{
  $str .= $v . "-" . $k;
}

Upvotes: 1

Ima
Ima

Reputation: 1109

Explode and array flip and then get array keys

$content_array = explode($file_contents);
//Remove duplicate entries
$flipped_array = array_flip($content_array);
file_put_contents('/path/to/file', implode("\n", array_keys($flipped_array));

Upvotes: 1

Related Questions