Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert files encoding

I have a PHP application who's files encoding is Greek ISO (iso-8859-7). I want to convert the files to utf-8 but simply saving the files with utf-8 isn't enough since the Greek texts get garbled. Is there an "automatic" method to do this so that I can completely convert my app's encoding without having to go through each file and rewrite the texts?

like image 950
bikey77 Avatar asked May 07 '26 00:05

bikey77


1 Answers

On a Linux system, if you are sure all files are currently encoded in ISO-8859-7, you can do this:

bash> find /your/path -name "*.php" -type f \
    -exec iconv "{}" -f ISO88597 -t UTF8 -o "{}.tmp" \; \
    -exec mv "{}.tmp" "{}" \;

This converts all PHP script files located in /your/path as well as all sub-directories. Remove -name "*.php" to convert all files.


Since you are under Windows, the easiest option would be a PHP script like this:

<?php
$path = realpath('C:\\your\\path');

$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($path), 
    RecursiveIteratorIterator::SELF_FIRST
);

foreach($iterator as $fileName => $file){
    if($file->isFile())
        file_put_contents(
            $fileName,
            iconv('ISO-8859-7', 'UTF-8', file_get_contents($fileName))
        );
}
like image 101
RandomSeed Avatar answered May 08 '26 14:05

RandomSeed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!