PHP problems with bars

0

Galera gave a researched but did not find something assertive about the error I'm having. I am doing the insertion of a csv file in the database, however the backslash has bugged the column separation a lot, a data from column c joins with the colune b.

Follow the code;

PHP

<?php
$file = "arquivo.csv"
if (($base = fopen($file, "r")) !== FALSE) {
  while (($data = fgetcsv($base, 0, ";")) !== FALSE) {
   //aqui realizo as correções de campos e insert na base utilizando $data[0], $data[1]...e assim por diante.
  }
}

ERROR RESULT

Failure happens with some specific records. Returning $sql looks something like this:

INSERT INTO (coluna1, coluna2, coluna3, coluna4, coluna5)
VALUES ("13", "asuahsuhas /";1435789;Nome do Usuario"","")

An example field that is giving error:

Uheuehuehwueheue /\

It seems to me that the backslash breaks the tab, and by understanding I would need to fix this in reading the file and not via replace. * NOTE: I can not move the .csv file base

Any tips?

    
asked by anonymous 24.02.2017 / 16:51

1 answer

0

I could not understand how it works, but replacing fgetcsv with the data below solved:

($data = fgetcsv($base, 0, ';' , '"' , '"')

So reading the csv file does not break, after that I just did the replacement of the bars.

<?php
  $file = "arquivo.csv"
  if (($base = fopen($file, "r")) !== FALSE) {

     while (($data = fgetcsv($base, 0, ';' , '"' , '"')) !== FALSE) {
       $data = str_replace('/', '', $data);
       $data = str_replace('\', '', $data);
       //aqui realizo as correções de campos e insert na base utilizando $data[0], $data[1]...e assim por diante.
     }
  }

Solved by then.

    
24.02.2017 / 22:42