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 相关文章推荐
Win2003服务器安全加固设置--进一步提高服务器安全性
May 23 PHP
php 读取文件乱码问题
Feb 20 PHP
深入PHP中的HashTable结构详解
Jun 13 PHP
PHP+Mysql树型结构(无限分类)数据库设计的2种方式实例
Jul 15 PHP
php中获取主机名、协议及IP地址的方法
Nov 18 PHP
PHP扩展模块memcached长连接使用方法分析
Dec 24 PHP
PHP实现数组array转换成xml的方法
Jul 19 PHP
PHP5.4起内置web服务器使用方法
Aug 09 PHP
PHP单例模式与工厂模式详解
Aug 29 PHP
php curl批处理实现可控并发异步操作示例
May 09 PHP
PHP常用字符串函数用法实例总结
Jun 04 PHP
thinkphp5 路由分发原理
Mar 18 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
剧场版动画《PSYCHO-PASS 3 FIRST INSPECTOR》3月27日日本上映!
2020/03/06 日漫
php返回json数据函数实例
2014/10/09 PHP
简单谈谈PHP面向对象之标识对象
2017/06/27 PHP
用php实现分页效果的示例代码
2020/12/10 PHP
Extjs学习笔记之二 初识Extjs之Form
2010/01/07 Javascript
javascript 事件查询综合 推荐收藏
2010/03/10 Javascript
将光标定位于输入框最右侧实现代码
2012/12/04 Javascript
实现checkbox全选、反选、取消JavaScript小脚本异常
2014/04/10 Javascript
jQuery中:animated选择器用法实例
2014/12/29 Javascript
Bootstrap表格和栅格分页实例详解
2016/05/20 Javascript
使用伪命名空间封装保护独自创建的对象方法
2016/08/04 Javascript
jQuery插件FusionCharts绘制的3D双柱状图效果示例【附demo源码】
2017/04/20 jQuery
JavaScript实现简单图片轮播效果
2017/08/21 Javascript
微信小程序中换行空格(多个空格)写法详解
2018/07/10 Javascript
AngularJS $http post 传递参数数据的方法
2018/10/09 Javascript
小程序日历控件使用方法详解
2018/12/29 Javascript
解决layui使用layui-icon出现默认图标的问题
2019/09/11 Javascript
解决vue自定义全局消息框组件问题
2019/11/22 Javascript
微信小程序背景音乐开发详解
2019/12/12 Javascript
解决VUE项目使用Element-ui 下拉组件的验证失效问题
2020/11/07 Javascript
Python实现自动添加脚本头信息的示例代码
2016/09/02 Python
Python 查看文件的编码格式方法
2017/12/21 Python
Python实现的登录验证系统完整案例【基于搭建的MVC框架】
2019/04/12 Python
python pymysql链接数据库查询结果转为Dataframe实例
2020/06/05 Python
python中使用.py配置文件的方法详解
2020/11/23 Python
西班牙家用电器和电子产品购物网站:Mi Electro
2019/02/25 全球购物
zooplus德国:便宜地订购动物用品、动物饲料、动物食品
2020/05/06 全球购物
.NET里面什么时候需要调用垃圾回收
2015/06/01 面试题
你们项目是如何进行变更控制的
2015/08/26 面试题
工商管理专业职业生涯规划
2014/01/01 职场文书
如何写好升职自荐信
2014/01/06 职场文书
网吧最新创业计划书范文
2014/03/27 职场文书
2014年党员公开承诺书范文
2014/03/28 职场文书
Vue接口封装的完整步骤记录
2021/05/14 Vue.js
Python图片检索之以图搜图
2021/05/31 Python
idea编译器vue缩进报错问题场景分析
2021/07/04 Vue.js