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 相关文章推荐
基于asp+ajax和数据库驱动的二级联动菜单
May 06 PHP
PHP下对数组进行排序的函数
Aug 08 PHP
解析:使用php mongodb扩展时 需要注意的事项
Jun 18 PHP
ThinkPHP多表联合查询的常用方法
Mar 24 PHP
PHP文件锁函数flock()详细介绍
Nov 18 PHP
php检查页面是否被百度收录
Oct 28 PHP
详解Window7 下开发php扩展
Dec 31 PHP
php实现转换html格式为文本格式的方法
May 16 PHP
php.ini中date.timezone设置详解
Nov 20 PHP
Zend Framework上传文件重命名的实现方法
Nov 25 PHP
PHP实现的同步推荐操作API接口案例分析
Nov 30 PHP
php实现的生成排列算法示例
Jul 25 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调用数据库的存贮过程!
2006/10/09 PHP
php 面试碰到过的问题 在此做下记录
2011/06/09 PHP
Destoon模板制作简明教程
2014/06/20 PHP
实例详解PHP中html word 互转的方法
2016/01/28 PHP
PHP实现的多维数组去重操作示例
2018/07/21 PHP
Laravel 实现在Blade模版中使用全局变量代替路径的例子
2019/10/22 PHP
Laravel 微信小程序后端实现用户登录的示例代码
2019/11/26 PHP
JS解密入门 最终变量劫持
2008/06/25 Javascript
javascript 页面只自动刷新一次
2009/07/10 Javascript
JavaScript 动态添加表格行 使用模板、标记
2009/10/24 Javascript
Javascript图像处理—平滑处理实现原理
2012/12/28 Javascript
JS的参数传递示例介绍
2014/02/08 Javascript
js中confirm实现执行操作前弹出确认框的方法
2014/11/01 Javascript
jQuery 遍历函数详解
2015/07/05 Javascript
模板视图和AngularJS之间冲突的解决方法
2016/11/22 Javascript
vue2.x 父组件监听子组件事件并传回信息的方法
2017/07/17 Javascript
jQuery实现的简单无刷新评论功能示例
2017/11/08 jQuery
使用Vue.js和Element-UI做一个简单登录页面的实例
2018/02/23 Javascript
详解create-react-app 2.0版本如何启用装饰器语法
2018/10/23 Javascript
微信小程序实现下拉框功能
2019/07/16 Javascript
[01:17]辉夜杯战队访谈宣传片—EHOME
2015/12/25 DOTA
Python实现身份证号码解析
2015/09/01 Python
python爬虫获取多页天涯帖子
2018/02/23 Python
Python制作exe文件简单流程
2019/01/24 Python
使用TensorFlow实现二分类的方法示例
2019/02/05 Python
解决Numpy中sum函数求和结果维度的问题
2019/12/06 Python
Python通过socketserver处理多个链接
2020/03/18 Python
Python多线程Threading、子线程与守护线程实例详解
2020/03/24 Python
长青弘远的面试题
2012/06/09 面试题
美容师的职业规划书
2013/12/27 职场文书
公司合并协议书范本
2014/09/30 职场文书
大学辅导员述职报告
2015/01/10 职场文书
酒店辞职信怎么写
2015/02/27 职场文书
2015年春训学习心得体会范文
2015/03/09 职场文书
2015年党风廉政建设目标责任书
2015/05/08 职场文书
2015年城管执法工作总结
2015/07/23 职场文书