0

I am trying to convert a php file (client.php) from utf-8 to iso-8859-1 and the following command does nothing on the file:

iconv -f UTF-8 -t ISO-8859-1 client.php

Upon execution the original file contents are displayed.

In fact, when I check for the file's encoding after executing iconv with:

file -I client.php

The same old utf-8 is shown:

client.php: text/x-php; charset=utf-8

Carles Andres
  • 1,761
  • 1
  • 15
  • 22
  • Are there any non-ASCII characters in the file where the encoding would actually make a difference? – deceze May 16 '12 at 10:56

3 Answers3

5

The iconv utility shall convert the encoding of characters in file from one codeset to another and write the results to standard output.

Here's a solution : Write stdout to a temporary file and rename the temporary file

iconv -f UTF-8 -t ISO_8859-1 client.php > client_temp.php && mv -f client_temp.php client.php
jackjr300
  • 7,111
  • 2
  • 15
  • 25
  • Hey @jackjr300 !! It worked!! IMHO, there should be a flag in iconv that allowed to overwrite the input file. Just an opinion. I will be aware the next time I read that results are written to standard output. – Carles Andres May 17 '12 at 14:42
  • @jackjr300 Could you help me with this problem:https://stackoverflow.com/questions/53573329/iconv-in-macos-doesnt-convert-file-from-us-ascii-to-utf-8 – user3760100 Dec 01 '18 at 17:40
1

ASCII, UTF-8 and ISO-8859 are 100% identical encodings for the lowest 128 characters. If your file only contains characters in that range (which is basically the set of characters you find on a common US English keyboard), there's no difference between these encodings.

My guess what's happening: A plain text file has no associated encoding meta data. You cannot know the encoding of a plain text file just by looking at it. What the file utility is doing is simply giving its best guess, and since there's no difference it prefers to tell you the file is UTF-8 encoded, which technically it may well be.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • My file contains some spanish characters that are not in the ASCII set. In fact, I noticed there was something wrong with the encoding when I rendered my file using: ``. Thanks anyway. – Carles Andres May 17 '12 at 14:34
0

In addition to jackjr300 with the following One-Liner you can do it for all php files in the current folder:

for filename in *.php; do  iconv -f ISO_8859-1 -t UTF-8 $filename > temp_$filename && mv -f ./temp_$filename ./$filename; done
Christian Michael
  • 2,128
  • 1
  • 19
  • 27