php txt删除重复行,php – 删除文本文件中的重复行
问题是每行末尾的新行字符.因为在最后一行的末尾没有新行字符,所以它与其他行不同.
因此,只需在读取文件时删除它们,然后在再次保存文件时添加:
$lines = file('test.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$lines = array_unique($lines);
file_put_contents('test.txt', implode(PHP_EOL, $lines));
如果你做:var_dump($lines);在file()调用之后你会看到它:
array(7) {
[0]=>
string(36) "new featuredProduct('', '21640'),
"
[1]=>
string(36) "new featuredProduct('', '24664'),
"
[2]=>
string(36) "new featuredProduct('', '22142'),
"
[3]=>
string(36) "new featuredProduct('', '22142'),
"
[4]=>
string(36) "new featuredProduct('', '22142'),
"
[5]=>
string(36) "new featuredProduct('', '22142'),
"
[6]=>
string(34) "new featuredProduct('', '22142'), "
//^^ See here ^ And here
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
