2011-05-13 57 views
1

這可能是一個棘手的問題,因此您可以獲得積分!在PHP中解析逗號分隔文件然後添加到數據庫

THIS IS .txt文件不是CSV

我試圖解析以下逗號分隔的文件轉換成可讀的表,然後導入到一切我的表。這裏是我得到的信息:

這是我從中導入的「模板」。 (這是文件中的第一行):

"Public/Private","Record Manager","Company","Contact","Address 1","Address 2","Address 3","City","State","Zip","Country","ID/Status","Phone","Fax","Home Phone","Mobile Phone","Pager","Salutation","Last Meeting","Last Reach","Last Attempt","Letter Date","Title","Assistant","Last Results","Referred By","User 1","User 2","User 3","User 4","User 5","User 6","User 7","User 8","User 9","User 10","User 11","User 12","User 13","User 14","User 15","Home Address 1","Home Address 2","Home City","Home State","Home Zip","Home Country","Alt Phone","2nd Contact","2nd Title","2nd Phone","3rd Contact","3rd Title","3rd Phone","First Name","Last Name","Phone Ext.","Fax Ext.","Alt Phone Ext.","2nd Phone Ext.","3rd Phone Ext.","Asst. Title","Asst. Phone","Asst. Phone Ext.","Department","Spouse","Record Creator","Owner","2nd Last Reach","3rd Last Reach","Web Site","Ticker Symbol","Create Date","Edit Date","Merge Date","E-mail Login","E-mail System" 

這裏是什麼樣的人通常會進入一個樣本:

"Public","John Doe」,"Einstein Construction","Bill Gates","3441 State Route 1","","","Somecity","CA","15212","","","724-555-2135","","","","","Jill","","4/18/2011","11/23/2009","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","Bill","Gregor","235","","","","","","","","","","Tom Jones","SuperMart","","","www.website.com","","11/14/2009","4/19/2011","","","" 

下面是我在數據庫中的字段(MySQL的):(顯然不的美元符號。)

 $rep 
     $date 
     $account 
     $areacode 
     $number 
     $address1 
     $address2 
     $city 
     $state 
     $zip 
     $country 
     $fax 
     $descmaker1 
     $descmaker2 
     $title 
     $email 
     $cvendor 
     $cequipment 
     $leaseexp1 
     $leaseexp2 
     $leaseexp3 
     $leaseexp4 
     $leaseexp5 
     $leaseexp6 
     $volume 
     $notes 
        $lastchange 

這就是我需要它們映射到..(我在右路,左邊的文件)

    Record Manager -> $rep 
(record manager i'd rather use our variable which is = $session->userinfo['fullname'] 
     Create Date -> $date (can the "/" be removed so its MMDDYYYY) 
     Company -> $account 
     Phone (can you explode the areacode off?) -> $areacode 
     Phone (and put the number after the areacode here?)-> $number 
     Address 1 -> $address1 
     Address 2 -> $address2 
     City -> $city 
     State -> $state 
     Zip -> $zip 
     Country -> $country 
     Fax -> $fax 
     Salutation -> $descmaker1 
     $descmaker2 
     Title -> $title 
     Email Login -> $email 
        Edit Date -> $lastchange 

我現在所擁有的(這真的不起作用)如下。它顯示了一個大混亂中的數據。

<?php 


$a = file_get_contents("upload/contacts.txt"); 
$a = str_replace(array("\r\n", "\t") , array("[NEW*LINE]" , "[tAbul*Ator]") , $a); 

print "<table border=\"1\">"; 
foreach(explode("[NEW*LINE]" , $a) AS $lines){ 
echo "<tr>"; 
foreach(explode("[tAbul*Ator]" , $lines) AS $li) { 
echo "<td>"; 
echo $li ; 
} 
echo "</tr>"; 

} 

echo "</table>"; 


?> 
+0

你可以使用Perl的? – HaloWebMaster 2011-05-13 19:04:48

+0

您是否試圖將csv文件數據導入到db表中?或者將csv文件數據呈現爲html表格? – 2011-05-13 19:06:58

+0

不知道我的服務器是否支持它。我不知道任何perl語法,所以你不得不忍受我。我有一個godaddy Linux主機帳戶。我想它會支持它! – 2011-05-13 19:07:31

回答

4
$file = fopen('mass-parse.txt', 'r'); 

while ($line = fgets($file)) { 
    list( $rep, $date, $account, $areacode, $number, 
      $address1, $address2, $city, $state, $zip, $country, 
      $fax, $descmaker1, $descmaker2, $title, $email, 
      $cvendor, $cequipment, $leaseexp1, $le, $leaseexp3, $leaseexp4, $leaseexp5, $leaseexp6, 
      $volume, $notes, $lastchange) = explode(',', str_replace('"','',$line)); 

    // do stuff with variables 
} 
+0

感謝您花時間寫這篇文章!正是我想要的,我現在可以修改。 – 2011-05-13 20:18:21

1

對於使用PHP解析CSV數據文件,看http://php.net/manual/en/function.fgetcsv.php

+0

不是CSV。正如它在問題中所說的那樣。 – 2011-05-13 19:12:12

+0

你有沒有嘗試使用fgetcsv解析文件? – 2011-05-13 19:15:08

+0

是的,它是一個CSV。每行一條記錄,用逗號分隔的行上有多個值...(C)omma(S)eparated(V)alues。 – 2011-05-13 19:16:29

2

對於在CSV文件中讀取(它是一個,無論.txt擴展名),你可以使用這個片段代替繁瑣的手工讀取和分裂:

$csv = array_map("str_getcsv", file("upload/contacts.txt")); 

Whatsmore,你可能需要調查LOAD DATA INFILE,例如MySQL經常可以直接導入這樣的數據結構。

LOAD DATA INFILE 'upload/contacts.txt' 
INTO TABLE contacts 
(rep, date, account, areacode, number, address1, city, ...) 

(你必須照顧與文件中的發生的匹配到合適的數據庫列名!)

+0

馬里奧,我認爲你的第一個例子可能會導致一個非常大的$ csv數組,並導致內存限制問題,如果被導入的文件很大。我忘記了LOAD DATA INFILE,優秀的提示 – 2011-05-13 19:24:16

+0

是的,的確如此。它容易消耗大量的內存(主要是由於PHP數組處理),所以這種懶惰的方法僅適用於中等大小的文件。 – mario 2011-05-13 19:27:42

+0

我如何才能將事件與您給我的例子相匹配? – 2011-05-13 20:08:11