Convert encoding from entire project to utf-8

Hi, I have a project made in Windows that uses the encoding of Windows 1252, and I need to convert my entire .php file to utf-8 encoding, because my database has all utf-8 encodings. Is there a way to do this using Linux commands or software?

+4
source share
2 answers

In the project root directory, use find (1) to list all *.php files and combine this with recode (1) to convert these files:

 find . -type f -name '*.php' -exec recode windows1252..utf8 \{} \; 

As an alternative to transcoding (1), you can also use iconv (1) to convert (for use with find above: iconv -f windows-1252 -t utf-8 -o \{} \{} ).

To work, you must either transcode or iconv. Both should be easily installed using the package manager on most modern systems.

+10
source

To convert a single file using Python (as I was asked ...)

 import codecs with codecs.open(filename_in, 'r', 'windows-1252') as fin: with codecs.open(filename_out, 'w', 'utf-8') as fout: for line in fin: fout.write(line) 

You can also encode utf-8 directly to a string without writing it to a file:

 utf8_line = line.encode('utf-8') 
+1
source

Source: https://habr.com/ru/post/1482049/


All Articles