CSV文件内容显示问题

时间:2010-06-02 06:39:45

标签: php csv

我想从csv文件中检索内容,因为我正在使用以下代码:

<?php
     $fo = fopen("record.csv", "rb+");
     while(!feof($fo)) {
          $contents[] = fgetcsv($fo,0,';');
     }
     print_r($contents);
     fclose($fo);

?>

但我的记录以下列格式显示:

䅲牡礊⠊††嬰崠㴾⁁牲慹ਠ†††
ਠ†††††⁛そ‽㸠"Search Transactions Results" ††††⤊ਠ†⁛ㅝ‽㸠䅲牡礊††††⠊††††††嬰崠㴾 ∀吀爀愀渀猀愀挀琀椀漀渀 䤀䐀∀Ⰰ∀刀攀昀攀爀攀渀挀攀 吀爀愀渀猀愀挀琀椀漀

我的csv文件格式:

"Search Transactions Results"
"Transaction ID","Reference Transaction ID","Date","Type","Subject","Item Number","Item Name","Invoice ID","Name","Email","Shipping Name","Shipping Address Line 1","Shipping Address Line 2","Shipping Address City","Shipping State/Province","Shipping Zip/Postal Code","Shipping Address Country","Shipping Method","Address Status","Contact Phone Number","Gross Amount","Receipt ID","Custom Field","Option 1 Name","Option 1 Value","Option 2 Name","Option 2 Value","Note","Auction Site","Auction User ID","Item URL","Auction Closing Date","Insurance Amount","Currency","Fees","Net Amount","Shipping & Handling Amount","Sales Tax Amount","To Email","Time","Time Zone"
"1T","",5/5/2010 2:10:44 PM,"Payment Processed","CFP Self Study Kit","1","CFP Self Study Kit","","User1","user1@gmail.com","","","","","","","","","N","","68.18","R1","","","","","","","","","",,"","USD","-2.62","65.56","0","0","receiver@gmail.com","01:40","Asia/Calcutta"
"2T","",5/19/2010 4:04:08 PM,"Payment Processed","CFP Self Study Kit","1","CFP Self Study Kit","","User2","user2@gmail.com","","","","","","","","","N","","68.18","R2","","","","","","","","","",,"","USD","-2.62","65.56","0","0","receiver@gmail.com","03:34","Asia/Calcutta"
"3T","1RT",5/19/2010 5:28:45 PM,"Currency Conversion Completed","","","",""," ","","","","","","","","","","N","","17492.6","","","","","","","","","","",,"","INR","0","17492.6","0","0","","04:58","Asia/Calcutta"
"4T","2RT",5/19/2010 5:28:45 PM,"Currency Conversion Completed","","","",""," ","","","","","","","","","","N","","-393.36","","","","","","","","","","",,"","USD","0","-393.36","0","0","","04:58","Asia/Calcutta"
"5T","",5/19/2010 5:28:45 PM,"Transfer to Bank Initiated","P1006","","P1006",""," ","","","","","","","","","","N","","-17492.6","","","","","","","","","","",,"","INR","0","-17492.6","0","0","","04:58","Asia/Calcutta"
"6T","",5/20/2010 5:38:02 PM,"Transfer to Bank Completed","P1006","","P1006",""," ","","","","","","","","","","N","","-17492.6","","","","","","","","","","",,"","INR","0","-17492.6","0","0","","05:08","Asia/Calcutta"
"7T","",5/21/2010 12:32:37 PM,"Payment Processed","FP - LVC Plus","","FP - LVC Plus","","User3","user3@gmail.com","User3","NEW DELHI","BEHIND KARNATAKA BANK LD","SOUTH","NEW DELHI","110023","IN","","N","","283.96","","","","","","","","","","",,"","USD","-9.95","274.01","0","0","receiver@gmail.com","00:02","Asia/Calcutta"
"8T","",5/25/2010 4:40:48 PM,"Transfer to Bank Initiated","P1006","","P1006",""," ","","","","","","","","","","N","","-12569.85","","","","","","","","","","",,"","INR","0","-12569.85","0","0","","04:10","Asia/Calcutta"
"9T","3RT",5/25/2010 4:40:48 PM,"Currency Conversion Completed","","","",""," ","","","","","","","","","","N","","-274.01","","","","","","","","","","",,"","USD","0","-274.01","0","0","","04:10","Asia/Calcutta"
"10T","4RT",5/25/2010 4:40:48 PM,"Currency Conversion Completed","","","",""," ","","","","","","","","","","N","","12569.85","","","","","","","","","","",,"","INR","0","12569.85","0","0","","04:10","Asia/Calcutta"
"11T","",5/26/2010 4:57:39 PM,"Transfer to Bank Completed","P1006","","P1006",""," ","","","","","","","","","","N","","-12569.85","","","","","","","","","","",,"","INR","0","-12569.85","0","0","","04:27","Asia/Calcutta"
"Total","-247.05 USD","-15.19","-262.24"
"Total","0.00 INR","0.00","0.00"

我想检索“Type”=“已处理付款”的记录。我想以密钥值格式检索内容,例如,交易ID-&gt; 1T因为我必须将此值存储在数据库中但显示不正确。 我无法找到相同的原因请帮助我。

由于

3 个答案:

答案 0 :(得分:0)

rb +中的b为fopen是二进制(输出是什么) - 只需使用r +;

答案 1 :(得分:0)

从PHP manual - 到PHP 4.3.2,对于区分二进制和文本模式的所有平台,默认模式设置为二进制。如果您在升级后遇到脚本问题,请尝试使用't'标志作为解决方法,直到您使脚本更具可移植性,如前所述。只要确保你使用rt +。请知道这是否有帮助。

答案 2 :(得分:0)

上述问题与编码有关。我复制了csv文件并粘贴在记事本中,另存为.csv和编码为ansi。现在内容即将开始

Array ( [0] => Array ( [0] => Search Transactions Results ) [1] => Array ( [0] => Transaction ID,"Reference Transaction ID","Date","Type","Subject","Item Number","Item Name","Invoice ID","Name","Email","Shipping Name","Shipping Address Line 1","Shipping Address Line 2","Shipping Address City","Shipping State/Province","Shipping Zip/Postal Code","Shipping Address Country","Shipping Method","Address Status","Contact Phone Number","Gross Amount","Receipt ID","Custom Field","Option 1 Name","Option 1 Value","Option 2 Name","Option 2 Value","Note","Auction Site","Auction User ID","Item URL","Auction Closing Date","Insurance Amount","Currency","Fees","Net Amount","Shipping & Handling Amount","Sales Tax Amount","To Email","Time","Time Zone" ) [2] => Array ( [0] => 1T,"",5/5/2010 2:10:44 PM,"Payment Processed","CFP Self Study Kit","1","CFP Self Study Kit","","User1","user1@gmail.com","","","","","","","","","N","","68.18","R1","","","","","","","","","",,"","USD","-2.62","65.56","0","0","receiver@gmail.com","01:40","Asia/Calcutta" ) [3] => Array ( [0] => 2T,"",5/19/2010 4:04:08 PM,"Payment Processed","CFP Self Study Kit","1","CFP Self Study Kit","","User2","user2@gmail.com","","","","","","","","","N","","68.18","R2","","","","","","","","","",,"","USD","-2.62","65.56","0","0","receiver@gmail.com","03:34","Asia/Calcutta" ) [4] => Array ( [0] => 3T,"1RT",5/19/2010 5:28:45 PM,"Currency Conversion Completed","","","",""," ","","","","","","","","","","N","","17492.6","","","","","","","","","","",,"","INR","0","17492.6","0","0","","04:58","Asia/Calcutta" ) [5] => Array ( [0] => 4T,"2RT",5/19/2010 5:28:45 PM,"Currency Conversion Completed","","","",""," ","","","","","","","","","","N","","-393.36","","","","","","","","","","",,"","USD","0","-393.36","0","0","","04:58","Asia/Calcutta" ) [6] => Array ( [0] => 5T,"",5/19/2010 5:28:45 PM,"Transfer to Bank Initiated","P1006","","P1006",""," ","","","","","","","","","","N","","-17492.6","","","","","","","","","","",,"","INR","0","-17492.6","0","0","","04:58","Asia/Calcutta" ) [7] => Array ( [0] => 6T,"",5/20/2010 5:38:02 PM,"Transfer to Bank Completed","P1006","","P1006",""," ","","","","","","","","","","N","","-17492.6","","","","","","","","","","",,"","INR","0","-17492.6","0","0","","05:08","Asia/Calcutta" ) [8] => Array ( [0] => 7T,"",5/21/2010 12:32:37 PM,"Payment Processed","FP - LVC Plus","","FP - LVC Plus","","User3","user3@gmail.com","User3","NEW DELHI","BEHIND KARNATAKA BANK LD","SOUTH","NEW DELHI","110023","IN","","N","","283.96","","","","","","","","","","",,"","USD","-9.95","274.01","0","0","receiver@gmail.com","00:02","Asia/Calcutta" ) [9] => Array ( [0] => 8T,"",5/25/2010 4:40:48 PM,"Transfer to Bank Initiated","P1006","","P1006",""," ","","","","","","","","","","N","","-12569.85","","","","","","","","","","",,"","INR","0","-12569.85","0","0","","04:10","Asia/Calcutta" ) [10] => Array ( [0] => 9T,"3RT",5/25/2010 4:40:48 PM,"Currency Conversion Completed","","","",""," ","","","","","","","","","","N","","-274.01","","","","","","","","","","",,"","USD","0","-274.01","0","0","","04:10","Asia/Calcutta" ) [11] => Array ( [0] => 10T,"4RT",5/25/2010 4:40:48 PM,"Currency Conversion Completed","","","",""," ","","","","","","","","","","N","","12569.85","","","","","","","","","","",,"","INR","0","12569.85","0","0","","04:10","Asia/Calcutta" ) [12] => Array ( [0] => 11T,"",5/26/2010 4:57:39 PM,"Transfer to Bank Completed","P1006","","P1006",""," ","","","","","","","","","","N","","-12569.85","","","","","","","","","","",,"","INR","0","-12569.85","0","0","","04:27","Asia/Calcutta" ) [13] => Array ( [0] => Total,"-247.05 USD","-15.19","-262.24" ) [14] => Array ( [0] => Total,"0.00 INR","0.00","0.00" ) [15] => ) 

但是我想要标题 - &gt;值的东西,而且只需要Type = Payment Processed

例如“交易ID” - &gt;'1T'