php去除每行的重复文本,php删除文本文件中重复行的方法
本文实例讲述了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删除文本文件中重复行的方法相关推荐
- python删除字符串中重复字符_删除字符串中重复字符python 用CAD怎么画DNA反向
用CAD怎么画DNA反向平行双螺旋结构绘螺旋线时,用选扭曲,确定顺时针. 画双头螺旋线时,第二根螺旋线底圆起点与第一根螺旋线底圆起点,可用角度分隔如180°.python去除文本中重复的字符串可有可无 ...
- 根据一个属性,剔除 Json 中重复元素(删除 JSON 中重复的部分)
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. [ {"data" : {"code" : "04 ...
- 删除链表重复节点 python_java删除链表中重复的节点(保留一个节点)
两种方法实现: package cn.exercise.list; import java.util.HashMap; /** * 删除链表重复节点(重复节点只保留一个) */ public clas ...
- java删除数组中重复元素
id="BAIDU_DUP_fp_iframe" src="https://pos.baidu.com/wh/o.htm?ltr="> > src= ...
- SQLServer删除表中重复记录
sqlserver删除表中的重复数据 SqlServer删除表中重复记录 转载链接:https://www.bbsmax.com/A/1O5Ee12G57/ SqlServer删除表中重复记录 重复记 ...
- 10-10 常见单词 : 访问项目Gutenberg(http://gutenberg.org/ ) , 并找一些你想分析的图书。 下载这些作品的文本文件或将浏览器中的原始文本复制到文本文件中。 你可
10-10 常见单词 : 访问项目Gutenberg(http://gutenberg.org/ ) , 并找一些你想分析的图书. 下载这些作品的文本文件或将浏览器中的原始文本复制到文本文件中. 你可 ...
- python删掉txt第一列_Python3.5 处理文本txt,删除不需要的行方法
这个问题是在问答里看到的,给了回答顺便在这里贴一下代码: #coding:utf-8 #python3.5.1 import re file_path0 = r'G:\任务20180312\test/ ...
- sql删除表中重复记录_SQL从SQL表中删除重复行的不同方法
sql删除表中重复记录 This article explains the process of performing SQL delete activity for duplicate rows f ...
- python删除重复文字_python如何删除文件中重复的字段
本文实例为大家分享了python如何删除文件中重复字段的具体代码,供大家参考,具体内容如下 原文件内容放在list中,新文件内容按行查找,如果没有出现在list中则写入第三个文件中. import c ...
最新文章
- 特斯拉FSD车端感知解析
- 鸟哥的Linux私房菜(基础篇)-第零章、计算机概论(零.5)
- Jupyter Nodebook添加代码提示(Vscode配置Jupyter Notebook运行.ipynb文件)
- Catch Overflow!
- 【转】MySQL的语句执行顺序
- 博为峰Java技术文章 ——JavaSE Swing JPanel III
- 【コンテンツ配信高速化 】
- java视频压缩 lz4_压缩包格式有哪些?
- [线性模型总结] 线性回归+方差分析+协方差分析+混合效应+面板数据模型
- 各型号iPhone的屏幕参数 逻辑分辨率 物理分辨率 - iOS Device Display Summary - 更新到iPhone 13系列
- java课程 数独 文库_数独java代码
- 员工“风清扬”感慨:为公司裁员结果把自己裁了,网友:真够狠的
- 支付宝退款流程 php,支付宝退款接口对接流程PHP语言
- 仅300员工却垄断全球,几亿一台还供不应求
- [渝粤教育] 西南科技大学 律师实务 在线考试复习资料2021版(1)
- 关于c++中的一个母牛生小牛的问题详细解答与体会
- Machine Learning 机器学习
- 人工智能技术并非已经完全成熟,而进入发展应用的阶段
- 可以在linux下运行的u盘制作工具,启动U盘创建工具(LiLi USB Creator)
- 拖拽式创建小程序原型 - 小piu神器 - 腾讯lowCode - 软件开发
热门文章
- pat 乙级 1027 打印沙漏(C++)
- 工业交换机的性能优势有哪些?
- 工业以太网交换机的安装流程详解
- 【渝粤教育】国家开放大学2019年春季 2712园艺基础 参考试题
- 【渝粤题库】国家开放大学2021春2094法理学题目
- FPGA初学者入门相关概念知识点
- linux正则表达式脚本实例,PowerShell中正则表达式使用例子
- c语言初始化字符串 函数 manment,[转载]3.09进程(C语言班最后一天的课程)
- hybbs接口php,HYBBS
- php mysql 随机字符串_MySQL_Mysql 自定义随机字符串的实现方法,前几天在开发一个系统,需要 - phpStudy...