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 相关文章推荐
一个阿拉伯数字转中文数字的函数
Oct 09 PHP
php与XML、XSLT、Mysql的结合运用实现代码
Nov 19 PHP
PHP正确解析UTF-8字符串技巧应用
Nov 07 PHP
PHP树的深度编历生成迷宫及A*自动寻路算法实例分析
Mar 10 PHP
SSO单点登录的PHP实现方法(Laravel框架)
Mar 23 PHP
PHP编译configure时常见错误的总结
Aug 17 PHP
php和vue配合使用技巧和方法
May 09 PHP
基于ThinkPHP5框架使用QueryList爬取并存入mysql数据库操作示例
May 25 PHP
解决在Laravel 中处理OPTIONS请求的问题
Oct 11 PHP
Laravel框架实现即点即改功能的方法分析
Oct 31 PHP
Laravel Eloquent分表方法并使用模型关联的实现
Nov 25 PHP
discuz论坛更换域名,详细文件修改步骤
Dec 09 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防注入安全代码
2008/04/09 PHP
PHP图片上传代码
2013/11/04 PHP
php中file_get_contents与curl性能比较分析
2014/11/08 PHP
php命令行模式代码实例详解
2021/02/26 PHP
潜说js对象和数组
2011/05/25 Javascript
IE8对JS通过属性和数组遍历解析不一样的地方探讨
2013/05/06 Javascript
js实现鼠标滚轮控制图片缩放效果的方法
2015/02/20 Javascript
基于JavaScript实现一定时间后去执行一个函数
2015/12/14 Javascript
javascript 实现动态侧边栏实例详解
2016/11/11 Javascript
JS操作input标签属性checkbox全选的实现代码
2017/03/02 Javascript
ES6学习笔记之正则表达式和字符串正则方法分析
2017/04/25 Javascript
微信小程序 flex实现导航实例详解
2017/04/26 Javascript
详解JavaScript数组过滤相同元素的5种方法
2017/05/23 Javascript
jQuery 控制文本框自动缩小字体填充
2017/06/16 jQuery
详解如何去除vue项目中的#——History模式
2017/10/13 Javascript
vue2.0 获取从http接口中获取数据,组件开发,路由配置方式
2019/11/04 Javascript
js实现视图和数据双向绑定的方法分析
2020/02/05 Javascript
node.js中 mysql 增删改查操作及async,await处理实例分析
2020/02/11 Javascript
[52:02]DOTA2-DPC中国联赛 正赛 Phoenix vs Dragon BO3 第二场 2月26日
2021/03/11 DOTA
python中while循环语句用法简单实例
2015/05/07 Python
Python实现抓取网页生成Excel文件的方法示例
2017/08/05 Python
Python算法之图的遍历
2017/11/16 Python
Python列表常见操作详解(获取,增加,删除,修改,排序等)
2019/02/18 Python
使用OpenCV实现人脸图像卡通化的示例代码
2021/01/15 Python
canvas需要在标签里直接定义宽高
2014/12/17 HTML / CSS
全球性的在线购物网站:Zapals
2017/03/22 全球购物
马来西亚最大的电器网站:Senheng
2017/10/13 全球购物
Chain Reaction Cycles芬兰:世界上最大的在线自行车商店
2017/12/06 全球购物
实习生的自我评价
2014/01/08 职场文书
初三学生评语大全
2014/04/24 职场文书
法律专业自荐信
2014/06/03 职场文书
上党课的心得体会
2014/09/02 职场文书
党员个人总结自评
2015/02/14 职场文书
多表查询、事务、DCL
2021/04/05 MySQL
MATLAB 全景图切割及盒图显示的实现步骤
2021/05/14 Python
关于Python中*args和**kwargs的深入理解
2021/08/07 Python