本文实例讲述了php删除文本文件中重复行的方法。分享给大家供大家参考。具体分析如下:

这个php函数用来删除文件中的重复行,还可以指定是否忽略大小写,和指定换行符

?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465/** * 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));  }

使用范例:

?12345678910111213// 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程序设计有所帮助。

小编推荐:欲学习电脑技术、系统维护、网络管理、编程开发和安全攻防等高端IT技术,请 点击这里注册账号,公开课频道价值万元IT培训教程免费学,让您少走弯路、事半功倍,好工作升职加薪!

免责声明:本站系公益性非盈利IT技术普及网,本文由投稿者转载自互联网的公开文章,文末均已注明出处,其内容和图片版权归原网站或作者所有,文中所述不代表本站观点,若有无意侵权或转载不当之处请从网站右下角联系我们处理,谢谢合作!

php去除每行的重复文本,php删除文本文件中重复行的方法相关推荐

  1. python删除字符串中重复字符_删除字符串中重复字符python 用CAD怎么画DNA反向

    用CAD怎么画DNA反向平行双螺旋结构绘螺旋线时,用选扭曲,确定顺时针. 画双头螺旋线时,第二根螺旋线底圆起点与第一根螺旋线底圆起点,可用角度分隔如180°.python去除文本中重复的字符串可有可无 ...

  2. 根据一个属性,剔除 Json 中重复元素(删除 JSON 中重复的部分)

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. [ {"data" : {"code" : "04 ...

  3. 删除链表重复节点 python_java删除链表中重复的节点(保留一个节点)

    两种方法实现: package cn.exercise.list; import java.util.HashMap; /** * 删除链表重复节点(重复节点只保留一个) */ public clas ...

  4. java删除数组中重复元素

    id="BAIDU_DUP_fp_iframe" src="https://pos.baidu.com/wh/o.htm?ltr="> > src= ...

  5. SQLServer删除表中重复记录

    sqlserver删除表中的重复数据 SqlServer删除表中重复记录 转载链接:https://www.bbsmax.com/A/1O5Ee12G57/ SqlServer删除表中重复记录 重复记 ...

  6. 10-10 常见单词 : 访问项目Gutenberg(http://gutenberg.org/ ) , 并找一些你想分析的图书。 下载这些作品的文本文件或将浏览器中的原始文本复制到文本文件中。 你可

    10-10 常见单词 : 访问项目Gutenberg(http://gutenberg.org/ ) , 并找一些你想分析的图书. 下载这些作品的文本文件或将浏览器中的原始文本复制到文本文件中. 你可 ...

  7. python删掉txt第一列_Python3.5 处理文本txt,删除不需要的行方法

    这个问题是在问答里看到的,给了回答顺便在这里贴一下代码: #coding:utf-8 #python3.5.1 import re file_path0 = r'G:\任务20180312\test/ ...

  8. sql删除表中重复记录_SQL从SQL表中删除重复行的不同方法

    sql删除表中重复记录 This article explains the process of performing SQL delete activity for duplicate rows f ...

  9. python删除重复文字_python如何删除文件中重复的字段

    本文实例为大家分享了python如何删除文件中重复字段的具体代码,供大家参考,具体内容如下 原文件内容放在list中,新文件内容按行查找,如果没有出现在list中则写入第三个文件中. import c ...

最新文章

  1. java memcached 存储对象_java – 从Memcache中获取低级别数据存储区实体对象时的慢速反序列化...
  2. 4.1.9 OS之文件系统的层次结构
  3. 前端代码有关搜索引擎的代码
  4. 如何使用Kubernetes官网的免费测试集群学习Kubernetes操作
  5. Linux的open函数的调用过程,Linux 中open系统调用实现原理
  6. Sublime Text 3
  7. 如何获取一张表的字段名
  8. 数据库递归查询(CET)
  9. 补:关于man关于SEE ALSO(参见)中代号与vim下常用命令
  10. dynamipsGUI+VMware
  11. MCU最强科普总结(收藏版)
  12. SAP案例教程CO成本会计后台配置
  13. 申通快递年营收253亿:净亏9亿 上年同期为盈利
  14. 王者荣耀AI绝悟完全体对战开启:英雄随便选,论文已被NeurIPS收录
  15. HTML5 Now: 深入了解HPolyfills
  16. 通俗理解TIM定时器并简单使用
  17. python学了没有用_如何用Python进行无监督学习
  18. R语言进行神经网络算法——RSNNS
  19. 小组项目--闲置物品交换系统-第一周
  20. 无法打开网页但可以登录电脑微信 解决办法

热门文章

  1. 从金蝶k3到金税盘_经典全套金蝶K3操作流程大全
  2. 将 Citavi 笔记按需要导出
  3. 【渝粤教育】国家开放大学2018年春季 7397-21T家庭教育咨询与辅导 参考试题
  4. 【渝粤题库】国家开放大学2021春2094法理学题目
  5. Zigbee 联盟更名为连接标准联盟
  6. 工业控制中无线局域网应用前景分析
  7. 无忧无盘服务器,无忧网维无盘系统新手快速部署.doc
  8. js中的数据类型分为两大类分别是什么_数据类型有这么重要吗?
  9. 怎么利用计算机计算潮流计算,电力系统潮流计算的目的是什么
  10. 中文转化成拼音_五笔已经淘汰,拼音到达瓶颈,百度重拳出击,全新输入方式来袭!...