0

I create a CSV file with an automated VB script of an Excel file. The file is encoded with latin_1. To import this file correctly into my MySQL database I need to convert the encoding of the file to UTF8.

It works if I save the file manually to UTF8, but I would like to have that automated with PHP.

The direction is XLS -> CSV -> MYSql. Everything runs on windows.

1
  • Are you have xls or csv originally? Commented Oct 2, 2016 at 21:15

1 Answer 1

1

This will convert the encoding of any text file from any encoding to UTF-8. Note that it will not work with an XLS file; you'd have to convert it to CSV first:

$pathToFile = '...';
$original = file_get_contents($pathToFile);
$originalEncoding = mb_detect_encoding($original);
$converted = mb_convert_encoding($original, 'UTF-8', $originalEncoding);
file_put_contents($pathToFile, $converted);

If you don't want to overwrite your original file, specify a different first argument for file_put_contents().

Sign up to request clarification or add additional context in comments.

1 Comment

There is an open source library know as PHPExcel that can parse XLS files without the need to convert to CSV manually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.