0

I have a weird CSV file. It has these characteristics:
- The first line (headers) are seperated by a semicolon ;
- All the rest is seperated by a comma ,
- The file is in ISO-8859-1 format

I can read the file, but I do an external conversion first, namely

"sed -i 's/;/,/g' " . $arg1;

That is depending on my linux shell and works fine locally. But I want it more robust and convert it within PHP so that the first line headers are sepeated with a comma too (and the output is UTF-8 if possible)

Any sugestions how to do this?

Thanks

user26432
  • 1
  • 2
  • Possible duplicate of [How to edit .csv header row](https://stackoverflow.com/questions/22618065/how-to-edit-csv-header-row) – IsThisJavascript Oct 22 '19 at 15:31
  • Also worth taking a look at; https://stackoverflow.com/questions/374425/convert-utf8-characters-to-iso-88591-and-back-in-php – IsThisJavascript Oct 22 '19 at 15:31
  • 2
    You really don't even need to edit the first row. You can specify the delimiter for [`fgetcsv`](https://www.php.net/manual/en/function.fgetcsv.php) (third option), so for the header row specify the semi-colon, and use comma for all the rest. – aynber Oct 22 '19 at 15:39
  • The C in CSV stands for 'Character', not comma, and Excel uses `;` which is a perfectly valid choice. Blanket-replacing commas and converting charsets is going to cause problems for you down the line. You _should_ be feeding the necessary extra parameters to your decoder so that it can just read the file as-is. – Sammitch Oct 22 '19 at 15:46
  • @Sammitch Actually, it really does mean Comma, just as a TSV is tab-separated values. Someone somewhere along the way decided to use other characters and still called it a CSV. – aynber Oct 22 '19 at 15:49

1 Answers1

1

If it's just the 1st line you could use a str_replace() on the 1st line. If the file is huge (otherwise why not use a text-editor), then you could progressively read it, to dump all the rest of the lines to the dest file as opposed to bringing the source file all into memory with file() or the like.

Edit:

Would love to hear the down voter's own solution.

Edit 2:

In PHP using str_replace() for the 1st line.

$lines = file($source, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$lines[0] = str_replace(";", ",", $lines[0]);


// DONE

// Output to dest or original
GetSet
  • 1,511
  • 2
  • 10
  • 13
  • I didn't downvote but I assume it's because you suggested running `str_replace` when there are far better solutions, such as what @aynber mentioned in the OP – IsThisJavascript Oct 22 '19 at 15:41
  • @isThisJavascript. The OP's requirements are that its affecting one line. Are you suggesting a performance hit on using str_replace() on one line? – GetSet Oct 22 '19 at 15:43
  • As I said, I didn't downvote I can only assume. I'm not inclined to upvote because I honestly think this was a poor question from the start. – IsThisJavascript Oct 22 '19 at 15:45
  • I understand. The idea in of itself -- making your own utilities -- I can respect that is why I answered. – GetSet Oct 22 '19 at 15:49