Blog de programación, errores, soluciones

Chose Language:
Author: Admin/Publisher |finished | checked

PHP fgetcsv function

The PHP fgetcsv function gets line from file pointer and parse for CSV fields

    resource $stream,
    ?int $length = null,
    string $separator = ",",
    string $enclosure = "\"",
    string $escape = "\\"
): array|false



$stream – is a data sream getted using fopen or another function to open files(popen(),fsockopen()), pointer to file that is opened.

example: $stream = fopen("mycsv.csv", "r"))

$length – max length expected from the row obtained from the csv file. This value is optional from PHP 8.0 (default case is the max value posible)

$separator -(optional) indicates the delimiter, the default value of the delimiter is "," because csv use , to separate different values.

$enclosure -(optional) default value "\""

$escape -(optional) define the escape character, "\\" is the default , in case $escape is equal "" this property will be disabled.

To better understand these values, we will see what a .csv format file would look like before moving on to the examples

Return values

Returns an indexed array on success, false on failure.

What is a CSV? (.csv file)

Generally is used in programs like excel and another similar programs to perform math operations, organize information, etc. This type of program permit us to export as csv.

The acronym CSV means comma-separated values, and literally is what it does.

For example, if we got the following table in excel:

toalla belagio55031
acolchado linea blanca330020
vela diseño20822

When we save the file as .csv, if we open it we will see something as follows:

toalla belagio,550,31
acolchado línea blanca,3300,20
vela diseño,208,22

Use example

Let’s start with a very simple example, and see how all have sense.

Obteniendo la primera linea
if (($gestor = fopen("cvs.csv", "r")) !== FALSE) {
	if (($datos = fgetcsv($gestor, 1000, ",")) !== FALSE)
array(5) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" } 

Ok, what is happening here?¿I’m not obtaining data? What is happening in this example, is that our table is not starting at position 0,0 or A,0(columns, rows) and thats why we are obtaining the first row with nothing inside it.

When y export my Excel file as .csv this one passed the rows with no value ass ,,,and columns ,,

Now we will see the example of the table that we see on, What is a CSV? (.csv file) but starting from 0,0

ejemplo fgetcsv
if (($gestor = fopen("cvs.csv", "r")) !== FALSE) {
	while (($datos = fgetcsv($gestor, 1000, ",")) !== FALSE) {
		$numero = count($datos);
		for ($c=0; $c < $numero; $c++) {
			echo $datos[$c];
		echo "<br/>";
toalla belagio55031
acolchado linea blanca330020
vela dise�o20822


I gave you 2 examples when save a file as .csv that is not at 0 ,0 we obtain data with white spaces

Create a table in Excel or a similar program that not start at 0,0 and save it as .csv, modify the code in example 2 to depurate the information and obtain only the data that is in the table

To do this exercise successfully, you will need to investigate a little from your part.

[arm_restrict_content plan=”2,” type=”show”] Admin? subsriberS? [armelse] This content is only visible for members – This part is under Test – please do not become a member. [/arm_restrict_content]
Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category