Marcell Nemeth
Marcell Nemeth

Reputation: 97

ANSI encoded file converting to UTF-8 encoded file with php script?

How can ANSI encoded file converting to UTF-8 encoded file with php, or any script, or any command line under linux?

Upvotes: 2

Views: 14184

Answers (2)

Ohgodwhy
Ohgodwhy

Reputation: 50787

Firstly, ANSI is not a type of character encoding. With ANSI, you need to find out what the encoding options are for the particular file that you're trying to read. First you should find out first if the file is already UTF-8 encoded, and if not, then simply encode it. Below, we check the encoding and if successful we return the file.

$output = false;
if( !mb_check_encoding( $myFile, 'UTF-8', true ) ):
   $output = mb_convert_encoding( $myFile, 'UTF-8' ); 
endif;

Then simply check if the encoding worked.

return $output ? $output : 'Failed encoding file!';

Upvotes: 9

archangel
archangel

Reputation: 127

Not my answer but highlighting one of the comments on Ohgodwhy's answer:

If you can use the command line then I would probably use iconv for this.

iconv -f iso-8859-1 -t utf-8 <infile >outfile

and of course adjust the variables accordingly. – Ohgodwhy

Upvotes: 1

Related Questions