2011-10-06 13 views
5

PHP के साथ CSV फ़ाइलों को पढ़ने के कुछ तरीके हैं। मैं विस्फोट फ़ंक्शन को प्रत्येक पंक्ति को सरणी में रखने के लिए विस्फोट अल्पविरामों का उपयोग करता था और डेटा के चारों ओर से किसी उद्धरण चिह्न को हटाने के लिए ट्रिम का उपयोग करता था। यह गंदा था ...PHP एक सीएसवी फ़ाइल प्रभावी ढंग से पढ़ रहा है

PHP 5 में वहाँ अब fgetcsv और है * str_getcsv * ... मैं अनुमान है कि इस इसके बारे में जाने के लिए सबसे अच्छा तरीका है इन दिनों तो मैं एक साथ कुछ कोड मार पड़ी है ..

$fp = fopen($csv_file['file_path'], 'r');  
while (($data = fgetcsv($fp, 0, "\r", '"', "\r")) !== FALSE) { 
    $num = count($data); 
    for ($c=0; $c < $num; $c++) { 
     print_r(str_getcsv($data[$c])); 
    } 
} 

ऐसा लगता है लेकिन क्या एक और असफल सुरक्षित दृष्टिकोण है? उदाहरण के लिए यह काम करता है कि लाइन ब्रेक \ n या \ r ...

कोई भी इनपुट जो आप दे सकते हैं वह शानदार होगा!

उत्तर

2

यह सीएसवी को नेस्टेड सरणी में परिवर्तित करता है। आपके साथ सौदा करने के लिए आसान हो सकता है:

<?php 
    /** 
    * 
    */ 
    function csv_entry_to_array(array $row) 
    { 
     $column_count = count($row); 
     $csv_column_values = array(); 

     // Loop through the columns of the current CSV entry and store its value 
     for ($column_index = 0; $column_index < $column_count; $column_index++) 
     { 
      // Store the value of the current CSV entry 
      $csv_column_values[] = $row[$column_index]; 
     } 

     // Return 
     return $csv_column_values;  
    } 

    /** 
    * @param string $input_file_name   Filename of input CSV 
    * @param boolean $include_header_in_output Flag indicating whether the first entry in the CSV is included in the output or not. 
    * @param integer $length      Must be greater than the longest line (in characters) to be found in the CSV file (allowing for trailing line-end characters). 
    *            It became optional in PHP 5. Omitting this parameter (or setting it to 0 in PHP 5.0.4 and later) the maximum line length is 
    *            not limited, which is slightly slower. 
    * @param string $delimeter     Set the field delimiter (one character only). 
    * @param string $enclosure     Set the field enclosure character (one character only). 
    * @param string $escape      Set the escape character (one character only). Defaults as a backslash. 
    * $return array        Nested indexed array representing the CSV. Empty array on error (e.g. input file missing, input file not a CSV). 
    */ 
    function csv_file_to_array($input_file_name, $include_header_in_output = TRUE, $length = 1000, $delimeter = ',', $enclosure = '"', $escape = '\\') 
    { 
     // NOTE: this attempts to properly recognize line endings when reading files from Mac; has small performance penalty 
     ini_set('auto_detect_line_endings', TRUE); 

     $csv_array = array(); 

     // Warnings are supressed, but that's OK since the code handles such warnings 
     if (($handle = @fopen($input_file_name, "r")) !== FALSE) 
     { 
      $row_counter  = 0; 

      // Iterate over the CSV entries 
      while (($row = fgetcsv($handle, $length, $delimeter, $enclosure, $escape)) !== FALSE) 
      {   
       if ($row_counter === 0 && $include_header_in_output === TRUE) 
       { 
        // This is the first row in the CSV and it should be included in the output 
        $csv_array[] = csv_entry_to_array($row);     
       } 
       else if ($row_counter > 0) 
       { 
        // This is a row in the CSV that needs to be stored in the return array 
        $csv_array[] = csv_entry_to_array($row); 
       } 

       $row_counter++; 
      } 

      // Close file handler 
      fclose($handle); 
     } 
     else 
     { 
      // Input file: some error occured 
      return array(); 
     } 

     return $csv_array; 
    } 
+0

मैंने ऊपर मारियो के लिए इसका भी उल्लेख किया लेकिन मुझे लगता है कि शायद मेरी सीएसवी फ़ाइल में कुछ गड़बड़ है ... यहां एक कॉय है bensinclair.co/import.csv ... जब मैं इस कोड का उपयोग अपने कोड के साथ करता हूं तो यह मेरे डेटा को आउटपुट करता है img192.imageshack.us/img192/2483/screenshot20111006at123b.png से ऊपर marios कोड के साथ क्या होता है मैंने मैक पर माइक्रोसॉफ्ट एक्सेल में सीएसवी फ़ाइल बनाई है। क्या मैं कुछ गलत कर रहा हूं या कोड में समायोजित करने की आवश्यकता है? –

+0

मुझे समस्या दिखाई देती है। ठीक कर देंगे। – StackOverflowNewbie

+0

मैंने कोड अपडेट किया है। जोड़ा गया 'ini_set (' auto_detect_line_endings ', TRUE); '। कोशिश करो। – StackOverflowNewbie

6

लाइन-वार में फ़ाइलों को पढ़ने के लिए एक फ़ंक्शन है: file(), जो दोनों लाइनब्रेक प्रकारों पर भी काम करता है।

और कम से कम विधि पूरे CSV फ़ाइल में पढ़ने के लिए है:

$data = array_map("str_getcsv", file($filename)); 

क्या आपको अपनी $num = count() बारे में था।

+0

हाय मारियो, उत्तर के लिए धन्यवाद! आपके द्वारा दिया गया कोड प्रत्येक सेल को प्रत्येक पंक्ति के बजाय अपने सरणी में एक सरणी में रखता है। तो अब इसमें हर पंक्ति के साथ हर सेल के साथ एक विशाल सरणी है। क्या मैं समझ रहा हूँ? मैं प्रत्येक पंक्ति को अपनी सरणी में कैसे तोड़ सकता हूं? –

+1

उपर्युक्त स्निपेट आपको एक द्वि-आयामी '$ डेटा [$ line] [$ पंक्ति]', आमतौर पर अनुमानित तालिका संरचना प्रदान करेगा। बिल्कुल यकीन नहीं है कि आप इसके बजाय क्या चाहते हैं। - यदि आप केवल एक बार प्रत्येक पंक्ति को देखना चाहते हैं, तो इसके बजाय एक foreach का उपयोग करें: 'foreach (फ़ाइल ($ fn) $ line के रूप में) {$ data = str_getcsv ($ line); } ' – mario

+0

मुझे लगता है कि शायद मेरी सीएसवी फ़ाइल में कुछ गड़बड़ है ... यहां http: //bensinclair.co/import.csv का एक कॉय है ... जब मैं इस फ़ाइल का उपयोग आपके कोड से करता हूं तो यह मेरे http : //img192.imageshack.us/img192/2483/screenshot20111006at123b.png –

संबंधित मुद्दे