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防注
Jan 15 PHP
php 静态变量与自定义常量的使用方法
Jan 26 PHP
php empty,isset,is_null判断比较(差异与异同)
Oct 19 PHP
linux下为php添加curl扩展的方法
Jul 29 PHP
php代码运行时间查看类代码分享
Aug 06 PHP
深入解析phpCB批量转换的代码示例
Jun 27 PHP
PHP中使用BigMap实例
Mar 30 PHP
Symfony2 session用法实例分析
Feb 04 PHP
ThinkPHP 模板substr的截取字符串函数详解
Jan 09 PHP
PHP多进程编程实例详解
Jul 19 PHP
Laravel 已登陆用户再次查看登陆页面的自动跳转设置方法
Sep 30 PHP
HTTP头隐藏PHP版本号实现过程解析
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获取当前所在目录位置的方法
2014/11/26 PHP
Zend Framework连接Mysql数据库实例分析
2016/03/19 PHP
CI框架文件上传类及图像处理类用法分析
2016/05/18 PHP
php版微信支付api.mch.weixin.qq.com域名解析慢原因与解决方法
2016/10/12 PHP
PHP合并数组的2种方法小结
2016/11/24 PHP
PHPExcel实现表格导出功能示例【带有多个工作sheet】
2018/06/13 PHP
PHP微信网页授权的配置文件操作分析
2019/05/29 PHP
JavaScript中几种常见排序算法小结
2011/02/22 Javascript
jquery 新建的元素事件绑定问题解决方案
2014/06/12 Javascript
浅谈JavaScript的Polymer框架中的事件绑定
2015/07/29 Javascript
学习使用AngularJS文件上传控件
2016/02/16 Javascript
bootstrap——bootstrapTable实现隐藏列的示例
2017/01/14 Javascript
AngularJS实现动态切换样式的方法分析
2018/06/26 Javascript
小程序scroll-view组件实现滚动的示例代码
2018/09/20 Javascript
解决element ui select下拉框不回显数据问题的解决
2019/02/20 Javascript
微信小程序非跳转式组件授权登录的方法示例
2019/05/22 Javascript
javascript 设计模式之组合模式原理与应用详解
2020/04/08 Javascript
Python 拷贝对象(深拷贝deepcopy与浅拷贝copy)
2008/09/06 Python
python开发之IDEL(Python GUI)的使用方法图文详解
2015/11/12 Python
详解Python装饰器由浅入深
2016/12/09 Python
python将文本中的空格替换为换行的方法
2018/03/19 Python
对Python 多线程统计所有csv文件的行数方法详解
2019/02/12 Python
python实现矩阵打印
2019/03/02 Python
python实现扫描ip地址的小程序
2019/04/16 Python
Python3 执行系统命令并获取实时回显功能
2019/07/09 Python
关于jupyter打开之后不能直接跳转到浏览器的解决方式
2020/04/13 Python
Python任务调度模块APScheduler使用
2020/04/15 Python
一文轻松掌握python语言命名规范规则
2020/06/18 Python
python实现企业微信定时发送文本消息的实例代码
2020/11/25 Python
Rhone官方网站:男士运动服装、健身服装和高级运动服
2019/05/01 全球购物
波兰化妆品和护肤品购物网站:eKobieca
2019/08/30 全球购物
中学生班主任评语
2014/01/30 职场文书
干部现实表现材料
2014/02/13 职场文书
机电专业求职信
2014/06/14 职场文书
Dashboard管理Kubernetes集群与API访问配置
2022/04/01 Servers
navicat 连接Ubuntu虚拟机的mysql的操作方法
2022/04/02 MySQL