PHP fgetcsv() ফাংশন
সংজ্ঞা ও ব্যবহার
fgetcsv() ফাংশন ফাইল ইন্ডেক্স থেকে একটি লাইন পড়ে এবং CSV ফিল্ডগুলি পার্স করে
সঙ্গে fgets() এরকমই, কিন্তু fgetcsv() পড়া হওয়া লাইনটিকে পার্স করে CSV ফরম্যাটের ফিল্ডগুলি খুঁজে পায় এবং এই ফিল্ডগুলির একটি আইন্ট ফিরায়
fgetcsv() এর ভুল হলে FALSE ফিরায়, ফাইলের শেষের দিকে পর্যন্ত
Note:থেকে PHP 4.3.5 থেকে, fgetcsv() এর কার্যকলাপ বাইনারি নিরাপদ
ব্যবহারিক ভাষা
fgetcsv(file,length,separator,enclosure)
পারামিটার | বর্ণনা |
---|---|
file | অপরিহার্য। নির্দিষ্ট করা হওয়া চিহ্নিত ফাইল |
length |
বাছাইযোগ্য। সর্বমাধ্যমিক লাইনের দৈর্ঘ্য নির্দিষ্ট করুন। এটি CVS ফাইলের সর্বমাধ্যমিক লাইনের দৈর্ঘ্য থেকে বেশি হতে হবে。 PHP 5-তে এই পারামিটারটি বাছাইযোগ্য। PHP 5-র পূর্বে এটি অপরিহার্য ছিল。 যদি এই পারামিটারটি অবমূল্যায়ন করা হয় (PHP 5.0.4-র পরের সংস্করণে 0 হিসাবে সংজ্ঞায়িত), তবে দৈর্ঘ্য সীমাহীন হবে, কিন্তু এটি কার্যকরিতা প্রভাবিত করতে পারে。 |
separator | Optional. Set the field delimiter (only one character is allowed), the default value is the comma. |
enclosure |
Optional. Set the field delimiter (only one character is allowed), the default value is the double quote. This parameter was added in PHP 4.3.0. |
Tips and Notes
Note:Empty lines in the CSV file will be returned as an array containing a single null field and will not be considered an error.
Note:This function is sensitive to locale settings. For example, if LANG is set to en_US.UTF-8, reading errors will occur for files with single-byte encoding.
Note:If PHP cannot recognize the line ending of a Macintosh file when reading a file, you can activate the auto_detect_line_endings runtime configuration option.
Instance
Example 1
<?php $file = fopen("contacts.csv","r"); print_r(fgetcsv($file)); fclose($file); ?>
CSV File:
George, John, Thomas, USA James, Adrew, Martin, USA
Output similar to:
Array ( [0] => George [1] => John [2] => Thomas [3] => USA )
Example 2
<?php $file = fopen("contacts.csv","r"); while(! feof($file)) { print_r(fgetcsv($file)); } fclose($file); ?>
CSV File:
George, John, Thomas, USA James, Adrew, Martin, USA
Output similar to:
Array ( [0] => George [1] => John [2] => Thomas [3] => USA Array ( [0] => James [1] => Adrew [2] => Martin [3] => USA )