服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - php删除文本文件中重复行的方法

php删除文本文件中重复行的方法

2020-09-21 19:46企鹅不笨 PHP教程

这篇文章主要介绍了php删除文本文件中重复行的方法,涉及php操作文本文件的相关技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了php删除文本文件中重复行的方法。分享给大家供大家参考。具体分析如下:

这个php函数用来删除文件中的重复行,还可以指定是否忽略大小写,和指定换行符

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
 * RemoveDuplicatedLines
 * This function removes all duplicated lines of the given text file.
 *
 * @param   string
 * @param   bool
 * @return  string
 */
function RemoveDuplicatedLines($Filepath, $IgnoreCase=false, $NewLine="\n"){
  if (!file_exists($Filepath)){
    $ErrorMsg = 'RemoveDuplicatedLines error: ';
    $ErrorMsg .= 'The given file ' . $Filepath . ' does not exist!';
    die($ErrorMsg);
  }
  $Content = file_get_contents($Filepath);
  $Content = RemoveDuplicatedLinesByString($Content, $IgnoreCase, $NewLine);
  // Is the file writeable?
  if (!is_writeable($Filepath)){
    $ErrorMsg = 'RemoveDuplicatedLines error: ';
    $ErrorMsg .= 'The given file ' . $Filepath . ' is not writeable!'
    die($ErrorMsg);
  }
  // Write the new file
  $FileResource = fopen($Filepath, 'w+');  
  fwrite($FileResource, $Content);   
  fclose($FileResource); 
}
 
/**
 * RemoveDuplicatedLinesByString
 * This function removes all duplicated lines of the given string.
 *
 * @param   string
 * @param   bool
 * @return  string
 */
function RemoveDuplicatedLinesByString($Lines, $IgnoreCase=false, $NewLine="\n"){
  if (is_array($Lines))
    $Lines = implode($NewLine, $Lines);
  $Lines = explode($NewLine, $Lines);
  $LineArray = array();
  $Duplicates = 0;
  // Go trough all lines of the given file
  for ($Line=0; $Line < count($Lines); $Line++){
    // Trim whitespace for the current line
    $CurrentLine = trim($Lines[$Line]);
    // Skip empty lines
    if ($CurrentLine == '')
      continue;
    // Use the line contents as array key
    $LineKey = $CurrentLine;
    if ($IgnoreCase)
      $LineKey = strtolower($LineKey);
    // Check if the array key already exists,
    // if not add it otherwise increase the counter
    if (!isset($LineArray[$LineKey]))
      $LineArray[$LineKey] = $CurrentLine;   
    else       
      $Duplicates++;
  }
  // Sort the array
  asort($LineArray);
  // Return how many lines got removed
  return implode($NewLine, array_values($LineArray)); 
}

使用范例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
// Example 1
// Removes all duplicated lines of the file definied in the first parameter.
$RemovedLinesCount = RemoveDuplicatedLines('test.txt');
print "Removed $RemovedLinesCount duplicate lines from the test.txt file.";
// Example 2 (Ignore case)
// Same as above, just ignores the line case.
RemoveDuplicatedLines('test.txt', true);
// Example 3 (Custom new line character)
// By using the 3rd parameter you can define which character
// should be used as new line indicator. In this case
// the example file looks like 'foo;bar;foo;foo' and will
// be replaced with 'foo;bar'
RemoveDuplicatedLines('test.txt', false, ';');

希望本文所述对大家的php程序设计有所帮助。

延伸 · 阅读

精彩推荐
  • PHP教程如何使用PHP批量去除文件UTF8 BOM信息

    如何使用PHP批量去除文件UTF8 BOM信息

    如果PHP文件头部包含BOM信息,就会输出一个空白,在很多时候会带来问题,比如我们session无法工作、cookie无法设置等等问题 ...

    PHP教程网3922020-05-13
  • PHP教程PHP中empty,isset,is_null用法和区别

    PHP中empty,isset,is_null用法和区别

    最近在阅读项目的源码,发现源码中就对empty、isset和is_null函数(语言特性)乱用,有的地方很明显的就挖坑了。不能正确的去理解这些东西,就很可能给...

    PHP教程网11562021-04-25
  • PHP教程php设计模式之单例模式实例分析

    php设计模式之单例模式实例分析

    这篇文章主要介绍了php设计模式之单例模式,实例分析了单例模式的原理与相关使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    蓝祖生5392020-09-06
  • PHP教程教你如何开启shopnc b2b2c 伪静态

    教你如何开启shopnc b2b2c 伪静态

    最近要给一个shopnc网站开启伪静态,用的是shopnc b2b2c,在网上搜索了好多shopnc开启伪静态的方法,但都是针对shaopnc c2c的,没有关于shopnc b2b2c的,最后终于...

    PHP开发实例3962020-08-01
  • PHP教程thinkphp 抓取网站的内容并且保存到本地的实例详解

    thinkphp 抓取网站的内容并且保存到本地的实例详解

    这篇文章主要介绍了thinkphp 抓取网站的内容并且保存到本地的实例详解的相关资料,需要的朋友可以参考下...

    modou9442021-06-21
  • PHP教程简单实现PHP留言板功能

    简单实现PHP留言板功能

    这篇文章主要教大家如何简单实现PHP留言板功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    尘中客10282021-04-08
  • PHP教程php 可变函数使用小结

    php 可变函数使用小结

    PHP 支持可变函数的概念。这意味着如果一个变量名后有圆括号,PHP 将寻找与变量的值同名的函数,并且尝试执行它。本文重点给大家介绍php 可变函数使用...

    烟熏妆5202019-09-27
  • PHP教程全面了解PHP中的全局变量

    全面了解PHP中的全局变量

    下面小编就为大家带来一篇全面了解PHP中的全局变量。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    PHP教程网6992021-01-28