我有一個約5000行的文本文件,每行約200個字符長。每行實際上包含6個不同的數據段,我一直在使用substr()
來分解。例如,在每行中,字符0-10包含客戶端#,字符10-20包含物質#等等。這一切都很好,運行得比我需要的還要快。PHP substr()沒有采取字符串的第一部分時真的很慢
當我的老闆告訴我客戶端號碼有4個前導零並且需要剝離時,我的問題就出現了。所以我想,沒問題 - 我只是改變了我的第一個substr()
函數從substr(0, 10)
(從0開始並取10個字符),並將其更改爲substr(4, 6)
(從第4個字符開始,僅取6),這將跳過4個前導零和I會很好去。
然而,當我改變了substr(0, 10)
到substr(4,6)
過程嘎然而止,並採取永遠完成。爲什麼是這樣?
這裏是我的代碼片段:
// open the file
$file_matters = fopen($varStoredIn_matters,"r") or exit("Unable to open file!");
// run until the end of the file
while(!feof($file_matters))
{
// place current line in temp variable
$tempLine_matters = fgets($file_matters);
// increment the matters line count
$linecount_matters++;
// break up each column
$clientID = trim(substr($tempLine_matters, 0, 10)); // THIS ONE WORKS FINE
//$clientID = trim(substr($tempLine_matters, 4, 6)); // THIS ONE MAKES THE PROCESS GRIND TO A HALT!!
$matterID = trim(substr($tempLine_matters, 10, 10));
//$matterID = trim(substr($tempLine_matters, 15, 5));
$matterName = trim(substr($tempLine_matters, 20, 80));
$subMatterName = trim(substr($tempLine_matters, 100, 80));
$dateOpen = trim(substr($tempLine_matters, 180, 10));
$orgAttorney = trim(substr($tempLine_matters, 190, 3));
$bilAttorney = trim(substr($tempLine_matters, 193, 3));
$resAttorney = trim(substr($tempLine_matters, 196, 3));
//$tolCode = trim(substr($tempLine_matters, 200, 3));
$tolCode = trim(substr($tempLine_matters, 200, 3));
$dateClosed = trim(substr($tempLine_matters, 203, 10));
// just does an insert into the DB using the variables above
}
你確定這是嗎?即它可以用於較小的數據文件嗎?什麼是錯誤?時限? –
我無法理解。根據實施,它不應該有真正的區別? – bwoebi
@AlmaDoMundo我幾乎肯定是這樣 - 更小的數據文件(即200行)處理沒有問題。由於這是我們的PHP配置中在我們的服務器上設置的最大執行時間,因此我在大約2分鐘後就會超時。如果我將'substr(4,6)'更改回'substr(0,10)',它將在大約3秒內運行5000行。 – FastTrack