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&amp;&amp;mysql)四
Oct 09 PHP
PHP 变量的定义方法
Jan 26 PHP
php in_array 函数使用说明与in_array需要注意的地方说明
Apr 13 PHP
php守护进程 加linux命令nohup实现任务每秒执行一次
Jul 04 PHP
批量去除PHP文件中bom的PHP代码
Mar 13 PHP
php将字符串转化成date存入数据库的两种方式
Apr 28 PHP
使用PHPMailer实现邮件发送代码分享
Oct 23 PHP
简单解析PHP程序的运行流程
Jun 23 PHP
mac系统下为 php 添加 pcntl 扩展
Aug 28 PHP
PHP获取中国时间(上海时区时间)及美国时间的方法
Feb 23 PHP
Laravel中log无法写入问题的解决
Jun 17 PHP
php中上传文件的的解决方案
Sep 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 CURL模拟GET及POST函数代码
2010/04/25 PHP
php 中文字符串首字母的获取函数分享
2013/11/04 PHP
php使用filter过滤器验证邮箱 ipv6地址 url验证
2013/12/25 PHP
php全角字符转换为半角函数
2014/02/07 PHP
php中限制ip段访问、禁止ip提交表单的代码分享
2014/08/22 PHP
php实现和c#一致的DES加密解密实例
2017/07/24 PHP
Jquery实现自定义窗口随意的拖拽
2014/03/12 Javascript
Js可拖拽放大的层拖动特效实现方法
2015/02/25 Javascript
基于jquery实现鼠标左右拖动滑块滑动附源码下载
2015/12/23 Javascript
Vue自定义指令详解
2017/07/28 Javascript
javascript代码优化的8点总结
2018/01/29 Javascript
React中的render何时执行过程
2018/04/13 Javascript
element-ui 限制日期选择的方法(datepicker)
2018/05/16 Javascript
vue修改对象的属性值后页面不重新渲染的实例
2018/08/09 Javascript
Python中一行和多行import模块问题
2018/04/01 Python
python pycurl验证basic和digest认证的方法
2018/05/02 Python
python使用pymongo操作mongo的完整步骤
2019/04/13 Python
Python基于内置库pytesseract实现图片验证码识别功能
2020/02/24 Python
keras 权重保存和权重载入方式
2020/05/21 Python
matplotlib更改窗口图标的方法示例
2021/02/03 Python
html5 canvas绘制矩形和圆形的实例代码
2016/06/16 HTML / CSS
美国娱乐和流行文化商品店:FYE
2017/09/14 全球购物
聪明的粉丝购买门票的地方:TickPick
2018/03/09 全球购物
eDreams加拿大:廉价航班、酒店和度假
2019/03/29 全球购物
澳大利亚优惠网站:Deals.com.au
2019/07/02 全球购物
员工培训心得体会
2013/12/30 职场文书
难忘的一课教学反思
2014/04/30 职场文书
电子商务专业毕业生求职信
2014/06/12 职场文书
机械专业应届毕业生自荐书
2014/06/12 职场文书
2014年合同管理工作总结
2014/12/02 职场文书
2015新学期开学寄语
2015/02/26 职场文书
2015年世界无烟日活动方案
2015/05/04 职场文书
师范生见习自我总结
2015/06/23 职场文书
工程进度款催款函
2015/06/24 职场文书
民政局2016年“六一”儿童节慰问活动总结
2016/04/06 职场文书
Python实现DBSCAN聚类算法并样例测试
2021/06/22 Python