欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php刪除文本文件中重復(fù)行的方法

 更新時間:2015年04月28日 09:28:38   作者:企鵝不笨  
這篇文章主要介紹了php刪除文本文件中重復(fù)行的方法,涉及php操作文本文件的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下

本文實例講述了php刪除文本文件中重復(fù)行的方法。分享給大家供大家參考。具體分析如下:

這個php函數(shù)用來刪除文件中的重復(fù)行,還可以指定是否忽略大小寫,和指定換行符

/**
 * 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程序設(shè)計有所幫助。

相關(guān)文章

  • php使用ffmpeg獲取視頻信息并截圖的實現(xiàn)方法

    php使用ffmpeg獲取視頻信息并截圖的實現(xiàn)方法

    這篇文章主要介紹了php使用ffmpeg獲取視頻信息并截圖的實現(xiàn)方法,實例分析了php操作視頻與圖像的相關(guān)技巧,需要的朋友可以參考下
    2016-05-05
  • 基于ThinkPHP實現(xiàn)批量刪除

    基于ThinkPHP實現(xiàn)批量刪除

    這篇文章主要介紹了基于ThinkPHP實現(xiàn)批量刪除的代碼實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2015-12-12
  • php用戶密碼加密算法分析【Discuz加密算法】

    php用戶密碼加密算法分析【Discuz加密算法】

    這篇文章主要介紹了php用戶密碼加密算法,較為詳細的分析了Discuz加密算法的原理,并結(jié)合實例形式對比了.net算法的實現(xiàn)方法總結(jié)了php進行用戶加密的流程與實現(xiàn)方法,需要的朋友可以參考下
    2016-10-10
  • PHP文件操作實現(xiàn)代碼分享

    PHP文件操作實現(xiàn)代碼分享

    PHP中提供了一系列的I/O函數(shù),能簡捷地實現(xiàn)我們所需要的功能,包括文件系統(tǒng)操作和目錄操作(如“復(fù)制[copy]”)。下面給大家介紹的是基本的文件讀寫操作:(1)讀文件;(2)寫文件;(3)追加到文件。
    2011-09-09
  • PHP7 新特性詳細介紹

    PHP7 新特性詳細介紹

    本文主要介紹PHP7 新特性的資料,這里整理了詳細的資料及簡單實現(xiàn)代碼幫助大家學(xué)習(xí)參考新特性的知識,有興趣的朋友可以參考下
    2016-09-09
  • PHP接入Apple對access_token/identityToken進行JWT驗證流程詳解

    PHP接入Apple對access_token/identityToken進行JWT驗證流程詳解

    JWT(JSON Web Token)是為了在網(wǎng)絡(luò)應(yīng)用環(huán)境間傳遞聲明而執(zhí)行的一種基于JSON的開放標(biāo)準(zhǔn)。本文將為大家介紹PHP如何實現(xiàn)JWT登錄鑒權(quán),需要的可以參考一下
    2022-09-09
  • PHP 圖像處理與SESSION制作超簡單驗證碼的方法示例

    PHP 圖像處理與SESSION制作超簡單驗證碼的方法示例

    這篇文章主要介紹了PHP 圖像處理與SESSION制作超簡單驗證碼的方法,結(jié)合實例形式詳細分析了PHP結(jié)合session繪制圖形驗證碼相關(guān)操作技巧,需要的朋友可以參考下
    2019-12-12
  • php校驗公鑰是否可用的實例方法

    php校驗公鑰是否可用的實例方法

    在本文里小編給大家整理的是一篇關(guān)于php校驗公鑰是否可用的知識點內(nèi)容,需要的朋友們參考下。
    2019-09-09
  • 解析php中獲取系統(tǒng)信息的方法

    解析php中獲取系統(tǒng)信息的方法

    本篇文章是對php中獲取系統(tǒng)信息的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • ubuntu 編譯安裝php 5.3.3+memcache的方法

    ubuntu 編譯安裝php 5.3.3+memcache的方法

    ubuntu 編譯安裝php 5.3.3+memcache的方法,需要的朋友可以參考下。
    2010-08-08

最新評論