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 相关文章推荐
Smarty Foreach 使用说明
Mar 23 PHP
基于ubuntu下nginx+php+mysql安装配置的具体操作步骤
Apr 28 PHP
Destoon模板制作简明教程
Jun 20 PHP
php采用curl模仿登录人人网发布动态的方法
Nov 07 PHP
php结合正则批量抓取网页中邮箱地址
May 19 PHP
PHP实现通过get方式识别用户发送邮件的方法
Jul 16 PHP
PHP中ID设置自增后不连续的原因分析及解决办法
Aug 21 PHP
php中序列化与反序列化详解
Feb 13 PHP
PHP中单例模式与工厂模式详解
Feb 17 PHP
Mac下php 5升级到php 7的步骤详解
Apr 26 PHP
PHP截取发动短信内容的方法
Jul 04 PHP
laravel在中间件内生成参数并且传递到控制器中的2种姿势
Oct 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
WINDOWS 2000下使用ISAPI方式安装PHP
2006/09/05 PHP
动态新闻发布的实现及其技巧
2006/10/09 PHP
有关于PHP中常见数据类型的汇总分享
2014/01/06 PHP
php数组索引的Key加引号和不加引号的区别
2014/08/19 PHP
php使用simplexml_load_file加载XML文件并显示XML的方法
2015/03/19 PHP
PHP守护进程化在C和PHP环境下的实现
2017/11/21 PHP
浅析jQuery(function(){})与(function(){})(jQuery)之间的区别
2014/01/09 Javascript
JavaScript中Function详解
2015/02/27 Javascript
jquery实现select下拉框美化特效代码分享
2015/08/18 Javascript
常用原生JS兼容性写法汇总
2016/04/27 Javascript
jquery.serialize() 函数语法及简单实例
2016/07/08 Javascript
用js动态添加html元素,以及属性的简单实例
2016/07/19 Javascript
JS实现图片局部放大或缩小的方法
2016/08/20 Javascript
jQuery.Form上传文件操作
2017/02/05 Javascript
javascript checkbox/radio onchange不能兼容ie8处理办法
2017/06/13 Javascript
jQuery.Form实现Ajax上传文件同时设置headers的方法
2017/06/26 jQuery
JavaScript canvas实现围绕旋转动画
2017/11/18 Javascript
vue使用vue-quill-editor富文本编辑器且将图片上传到服务器的功能
2021/01/13 Vue.js
[09:43]DOTA2每周TOP10 精彩击杀集锦vol.5
2014/06/25 DOTA
Python中的下划线详解
2015/06/24 Python
django 2.0更新的10条注意事项总结
2018/01/05 Python
Python实现输出某区间范围内全部素数的方法
2018/05/02 Python
django 使用全局搜索功能的实例详解
2019/07/18 Python
PyCharm更改字体和界面样式的方法步骤
2019/09/27 Python
基于python实现雪花算法过程详解
2019/11/16 Python
pytorch之inception_v3的实现案例
2020/01/06 Python
python线程里哪种模块比较适合
2020/08/02 Python
python能做哪些生活有趣的事情
2020/09/09 Python
解决python3.x安装numpy成功但import出错的问题
2020/11/17 Python
使用CSS3来实现滚动视差效果的教程
2015/08/24 HTML / CSS
印度尼西亚最大和最全面的网络商城:Blibli.com
2017/10/04 全球购物
2015年预算员工作总结
2015/05/14 职场文书
2015年学校减负工作总结
2015/05/19 职场文书
学生会干部任命书
2015/09/21 职场文书
Javascript中的解构赋值语法详解
2021/04/02 Javascript
zabbix监控mysql的实例方法
2021/06/02 MySQL