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 伪造本地文件包含漏洞的代码
Nov 03 PHP
PHP仿盗链代码
Jun 03 PHP
解析thinkphp基本配置 convention.php
Jun 18 PHP
深入PHP许愿墙模块功能分析
Jun 25 PHP
php使用正则表达式提取字符串中尖括号、小括号、中括号、大括号中的字符串
Apr 05 PHP
PHP随机生成唯一HASH值自定义函数
Apr 20 PHP
Yii针对添加行的增删改查操作示例
Oct 18 PHP
Yii统计不同类型邮箱数量的方法
Oct 18 PHP
php in_array() 检查数组中是否存在某个值详解
Nov 23 PHP
php和asp语法上的区别总结
May 12 PHP
php中的buffer缓冲区用法分析
May 31 PHP
Thinkphp 框架配置操作之动态配置、扩展配置及批量配置实例分析
May 15 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 array_multisort()函数的使用札记
2011/07/03 PHP
php管理nginx虚拟主机shell脚本实例
2014/11/19 PHP
php实现搜索一维数组元素并删除二维数组对应元素的方法
2015/07/06 PHP
php+ajax制作无刷新留言板
2015/10/27 PHP
PHP全局使用Laravel辅助函数dd
2019/12/26 PHP
PHP高并发和大流量解决方案整理
2021/03/09 PHP
JavaScript 高效运行代码分析
2010/03/18 Javascript
用js实现的自定义的对话框的实现代码
2010/03/21 Javascript
JQuery插件Style定制化方法的分析与比较
2012/05/03 Javascript
把jQuery的类、插件封装成seajs的模块的方法
2014/03/12 Javascript
JavaScript检查数字是否为整数或浮点数的方法
2015/06/09 Javascript
Jquery日历插件制作简单日历
2015/10/28 Javascript
JavaScript原型继承_动力节点Java学院整理
2017/06/30 Javascript
javascript实现Java中的Map对象功能的实例详解
2017/08/21 Javascript
vue中component组件的props使用详解
2017/09/04 Javascript
VUE element-ui 写个复用Table组件的示例代码
2017/11/18 Javascript
vue: WebStorm设置快速编译运行的方法
2018/10/18 Javascript
在Python中使用CasperJS获取JS渲染生成的HTML内容的教程
2015/04/09 Python
Python中列表、字典、元组数据结构的简单学习笔记
2016/03/20 Python
python 动态生成变量名以及动态获取变量的变量名方法
2019/01/20 Python
Python何时应该使用Lambda函数
2019/07/02 Python
python的debug实用工具 pdb详解
2019/07/12 Python
Python解析json代码实例解析
2019/11/25 Python
Python利用Xpath选择器爬取京东网商品信息
2020/06/01 Python
Python3利用scapy局域网实现自动多线程arp扫描功能
2021/01/21 Python
html5指南-1.html5全局属性(html5 global attributes)深入理解
2013/01/07 HTML / CSS
iframe与window.onload如何使用详解
2020/05/07 HTML / CSS
如何通过jdbc调用存储过程
2012/04/19 面试题
数学系毕业生的自我评价
2014/01/10 职场文书
平面设计求职信
2014/03/10 职场文书
如何写好建议书
2014/03/13 职场文书
房产授权委托书范本
2014/09/22 职场文书
领导班子党的群众路线对照检查材料
2014/09/25 职场文书
企业法人代表授权委托书
2014/10/02 职场文书
2015年导购员工作总结
2015/04/25 职场文书
党支部半年考察意见
2015/06/01 职场文书