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


Posted in PHP onApril 28, 2015

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

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

/**
 * 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));  
}

使用范例:

// 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分页函数
Jul 08 PHP
libmysql.dll与php.ini是否真的要拷贝到c:\windows目录下呢
Mar 15 PHP
php模板中出现空行解决方法
Mar 08 PHP
php 模拟POST提交的2种方法详解
Jun 17 PHP
使用PHP实现阻止用户上传成人照片或者裸照
Dec 25 PHP
PHP+APACHE实现网址伪静态
Feb 22 PHP
php检索或者复制远程文件的方法
Mar 13 PHP
PHP闭包函数详解
Feb 13 PHP
PHP对称加密函数实现数据的加密解密
Oct 27 PHP
thinkPHP js文件中U方法不被解析问题的解决方法
Dec 05 PHP
php面向对象程序设计入门教程
Jun 22 PHP
php输出反斜杠的实例方法
Sep 19 PHP
php实现简单的语法高亮函数实例分析
Apr 27 #PHP
php转换颜色为其反色的方法
Apr 27 #PHP
PHP结合jQuery.autocomplete插件实现输入自动完成提示的功能
Apr 27 #PHP
PHP+jQuery+Ajax实现用户登录与退出
Apr 27 #PHP
php使用cookie实现记住用户名和密码实现代码
Apr 27 #PHP
php使用cookie实现记住登录状态
Apr 27 #PHP
php curl请求信息和返回信息设置代码实例
Apr 27 #PHP
You might like
用PHP读取和编写XML DOM的实现代码
2011/02/03 PHP
使用PHP进行微信公众平台开发的示例
2015/08/21 PHP
PHP+AJAX实现投票功能的方法
2015/09/28 PHP
PHP操作MySQL中BLOB字段的方法示例【存储文本与图片】
2017/09/15 PHP
PHP5.6读写excel表格文件操作示例
2019/02/26 PHP
php多进程应用场景实例详解
2019/07/22 PHP
通用javascript脚本函数库 方便开发
2009/10/13 Javascript
jquery插件 autoComboBox 下拉框
2010/12/22 Javascript
Extjs3.0 checkboxGroup 动态添加item实现思路
2013/08/14 Javascript
如何在父窗口中得知window.open()出的子窗口关闭事件
2013/10/15 Javascript
优化javascript的执行效率一些方法总结
2013/12/25 Javascript
window.open 以post方式传递参数示例代码
2014/02/27 Javascript
javascript中加号(+)操作符的一些神奇作用
2014/06/06 Javascript
javascript定义变量时有var和没有var的区别探讨
2014/07/21 Javascript
jQuery实现textarea自动增长宽高的方法
2015/12/18 Javascript
JavaScript 是什么意思
2016/09/22 Javascript
vue中用H5实现文件上传的方法实例代码
2017/05/27 Javascript
ng-zorro-antd 入门初体验
2018/12/03 Javascript
Angular封装表单控件及思想总结
2019/12/11 Javascript
Pandas探索之高性能函数eval和query解析
2017/10/28 Python
树莓派+摄像头实现对移动物体的检测
2019/06/22 Python
Python Django中间件,中间件函数,全局异常处理操作示例
2019/11/08 Python
Python 为什么推荐蛇形命名法原因浅析
2020/06/18 Python
Pytest单元测试框架如何实现参数化
2020/09/05 Python
HTML5手机端弹出遮罩菜单特效代码
2016/01/27 HTML / CSS
AC Lens:购买隐形眼镜
2017/02/26 全球购物
美国最大的袜子制造商和零售商:Renfro Socks
2017/09/03 全球购物
你对IPv6了解程度
2016/02/09 面试题
银行会计主管岗位职责
2014/10/01 职场文书
党员群众路线学习心得体会
2014/11/04 职场文书
2014年医院工作总结
2014/11/20 职场文书
冰雪公主观后感
2015/06/16 职场文书
谢师宴家长致辞
2015/07/27 职场文书
2016年五一促销广告语
2016/01/28 职场文书
带你彻底理解JavaScript中的原型对象
2021/04/14 Javascript
如何使用SQL Server语句创建表
2022/04/12 SQL Server