Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

php - Remove duplicate lines in a text file

I am in need to open a text file (file.txt) which contains data in the following format :-

dave : 50lb : hlof
jimmy : 55lb : okay
dave : 12lb : krsho

I want to remove lines that has duplicate start word So the result would look like this :-

dave : 50lb : hlof
jimmy : 55lb : okay

I've been thinking about using array_unique but didn't worked so any idea how it can be done .. maybe can be with regex !

Update

my try was

$lines = file('file.txt');
$lines = array_unique($lines);
file_put_contents('file.txt', implode($lines));

but didn't worked cause it only compare the whole line if not the same will then consider it different

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This should work for you:

(Here I first go through each element of $lines with array_map() then I explode() every value with : and return the first element. After this I use array_intersect_key() with the created array from before where I use array_unique() to get the unique keys and get the intersect with the full array)

<?php

    $lines = file("test.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

    $lines = array_intersect_key($lines, array_unique(array_map(function($v){
        return trim(explode(":", $v)[0]);
    }, $lines)));

    print_r($lines);

?>

Output:

Array ( [0] => dave : 50lb : hlof [1] => jimmy : 55lb : okay )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...