2011-06-30 68 views
-1

嗨即時嘗試按降序讀取文件。 我想最後的10個字從文件呼應
預期的結果:以降序閱讀文件php

博恩崔西,博恩崔西,德賴歇 麻袋,德賴歇麻袋,德賴歇 麻袋,電機由查爾斯小號 西斯金德第二E,測試日politica 財政,gigantomastia,gigantomastia ,, 一個,

文件我想讀:

尋找醫生,找到一位醫生,左手手指右手技術,適應性強,適應性強,適應性強,能夠準確定位,裂紋束,侏儒兔,probabilidad estadistica,kamsutra bangla,狗的力量,狗的力量,prinsip kerja uji ninhidrin,letramania 3,gre,gre,prinsip kerja uji ninhidrin,prinsip kerja uji ninhidrin,人工智能現代方法,配置SAP erp財務和控制,氣體春天,卡波利奧,藍領男子,caligrafia,wonderlic,婦女和減肥tamasha,婦女和減肥tamasha,vivir amar y aprender leo buscaglia,vivir amar y aprender leo buscaglia,wonderlic,plan de manejo ambiental,calibra o de manometros,curso de carpinteria,secreto industrial,secreto industrial,deneme,elementos secundarios de un triangulo,imperio carolingio,caligrafia,construir en lo construi do,plan de manejo ambiental,lisboa,lisboa secreta,modelo de contrato secreto industrial,el conde de montecristo,metode titrasi formol,metode titrasi formol,probabilidad estadistica,probabilidad estadistica,islam akbar shah najeebabadi,caligrafia,caligrafia,conversacion en la catedral,brian tracy,brian tracy,der reiche sack,der reiche sack,der reiche sack,由charles s siskind second e執行的電機,test de politica fiscal,gigantomastia,gigantomastia,找醫生,找醫生,用手指技巧右手在左上方,la empresa adaptable,la empresa adaptable en la era de la informaci n,la pobre mia,probabilidad estadistica,crack beam,dwarf rabbit,probabilidad estadistica,kamsutra bangla,the power of the dog,the power of the dog ,prinsip kerja uji ninhidrin,letramania 3,gre,gre,prinsip kerja uji ninhidrin,prinsip kerja uji ninhidrin,人工智能一種現代方法,配置SAP erp財務和控制,gas spri ng,卡佩里奧,藍領男子,caligrafia,wonderlic,女性和減肥tamasha,女性和減肥tamasha,vivir amar y aprender leo buscaglia,vivir amar y aprender leo buscaglia,wonderlic,plan de manejo ambiental,calibra o de manometros,curso de carpinteria,secreto industrial,secreto industrial,deneme,elementos secundarios de un triangulo,imperio carolingio,caligrafia,construir en lo construido,plan de manejo ambiental,lisboa,lisboa secreta,modelo de contrato secreto industrial,el conde de montecristo ,metode titrasi formol,metode titrasi formol,probabilidad estadistica,probabilidad estadistica,伊斯蘭教的歷史akbar shah najeebabadi,caligrafia,caligrafia,conversacion en la catedral,布萊恩特雷西,布萊恩特雷西,德萊西麻袋,德萊西麻袋,德萊西麻袋,電氣機器由查爾斯siskind秒e,測試de政治財政,gigantomastia,gigantomastia ,,一

+2

這看起來太像一個典型的鏈接農場垃圾頁面,我認爲回答這個。 –

+0

問題在哪裏?預期的結果是什麼? –

+0

預期結果增加 – cute

回答

1
$file = "File contents"; //File get contents or anything else here. 

    $array = explode(",", $file); 

$array = array_slice($array, -10, 10); //Starting from Last 10th element, get Ten elements. 
$string = implode(", ", $array); 
echo $array; 

編輯:

改變以消除環路和計數等實施

+1

如果輸入<11個單詞就會中斷。 – codaddict

+0

您不檢查數組是否有較少的條目。 看到我的答案。 – ComFreek

4

如果該文件將不會太大,你可以簡單地閱讀所有,然後刪除數據時,你不需要:

$content = file_get_contents($filename); // $filename is the file to read 
$chunks = explode($delimiter, $content); // $delimiter is your word separator 

$chunks = array_slice($chunks, -$n);  // $n is the number of words to keep from the end of the file 
              // NOTE : -$n ! 

如果該文件將增長超過合理的規模被加載到內存中,你可以分塊閱讀。喜歡的東西(未經測試):

function getLastTokens($filename, $n, $delimiter) { 
    $offset = filesize($filename); 
    $chunksize = 4096; // 4K chunk 

    if ($offset <= $chunksize * 2) { 
     // our one liner here because the file is samll enough 
     $tokens = explode($delimiter, file_get_contents($filename)); 

    } else { 
     $tokens = array(); 

     $fp = fopen($filename, 'r'); 

     $chunkLength = 0; 
     while (count($tokens) < $n && $offset > 0) { 
     $lastOffset = $offset; 
     $offset -= $chunksize; 
     if ($offset < 0) $offset = 0;    // can't seek before first byte 

     $chunkLength += ($lastOffset - $offset); // how much to read 

     fseek($fp, $offset); 
     $data = fread($fp, $chunkLength);   // read the next (previous) chunk 

     if (($pos = strpos($data, $delimiter)) !== false) { 
      $chunkLength = 0;      // reset chunk size to read next time 
      $offset += $pos; 

      $data = explode($delimiter, substr($data, $pos + 1)); 
      array_unshift($data, & $tokens);  // make $tokens the $data array's first element 

      // with the last line, this is equivalent to 
      // array_push($tokens, $data[1], $data[2], $data[3], ....) 
      call_user_func_array('array_push', $data); 
     } 
     } 
     fclose($fp); 

    } 

    fclose($fp); 

    return array_slice($tokens, -$n); 
} 
0
$text = file_get_contents($file);    //get contents of file 
$words = explode(',', $text);     //split into array 

if (($length = count($words) < 10) { 
    $lastWords = $words;      //shorter than 10 so return all 
} else { 
    for ($i = $length-11, $i < $length; $i++ { //loop through last 10 words 
     $lastWords[] = $words[$i];    //add to array 
    } 
} 

$str = implode(',', $lastWords);    //change array back into a string 
echo $str;