0

I am trying to extract some information from a csv file. What I want is to extract information only from the lines where this information is from the email and ID.

Previously I had no problems extracting information from csv files via php, however this csv file that I am using has the information in a very particular way.

Here I show part of the contents of the csv file:

enter image description here

As you can see, the information entered is not in a conventional format.

Using this small code:

    $file = fopen("mails.csv","rb");

    while(feof($file) == false)
    {

    echo fgets($file). "<br />";


    }

    fclose($file);

I get this on screen: enter image description here

the file is more extensive, has more data. What I have shown is only a small part of the file, used as an example.

What I want to do is extract information from the lines where the radio type input is because there is the email, which is the information that I really want to extract. I have tried with the conventional PHP functions to extract information from csv files but they do not work for me.

they throw me multiple errors and I can not get the information only from the lines that have the emails and save those emails in an array.

Alguna idea que puedan darme para obtener solo las lineas que tienen los inputs radio y extraer de allí el correo electronico de cada linea y guardarlo en un array?

Any help they can give me I would appreciate it.

Carmen B.
  • 349
  • 7
  • 22
  • I think this has been answered already.. check below https://stackoverflow.com/questions/33865113/extract-email-address-from-string-php – Emz Aug 16 '18 at 06:35
  • It helps me solving some part of my issue, thanks. Now I'm trying to get just only the lines with input radios codes, to get the email at the end. – Carmen B. Aug 16 '18 at 06:50
  • Are those lines of data all in one cell or is the data in seperate cells – RiggsFolly Aug 16 '18 at 07:43
  • in each line the data is all in one cell, that's the reason I got some problems to extract the information. – Carmen B. Aug 16 '18 at 12:02

1 Answers1

1

Assuming that your format never changes (including for the invalid lines that you wish to ignore), the below would work.

Note: I have not tested this code so use this as a pointer and make adjustments as required.

$file = fopen("mails.csv","rb");

while(feof($file) == false){
    $contents = fgets($file);
    if (substr($contents,0,1) != "#"){
        $val = explode(",", $contents);
        echo $val[0]. "<br />";
    }
}

fclose($file);
Ravi
  • 868
  • 2
  • 10
  • 21