NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
Automatically setting the encoding for many files


Joined: 08 Mar 2006
Posts: 63
Reply with quote
As there isn't a simple way of changing the character encoding for a file in PhpED, I made a small script to automatically change the encoding for UTF-8 encoded files in an entire directory structure. Perhaps it might be useful for someone else as well.

I wrote it in PHP since that can work as a scripting language of sorts from within PhpED. Save the code below to a PHP file somewhere and add a new Shell Command like this under Tools / Integration:

@php@ -n -f c:\thepath\thename.php c:\webroot

If you keep the script in the active project, you could instead of hardcoding the web root use a custom variable like @ProjPath(web_root)@. Run the script from Tools / Scripts. You then have to quit PhpED and rename the enc_file manually (remove the .new), since PhpED will overwrite this file when the program is closed. Perhaps there is a way to avoid this?

Code:
<?php

class SetEncoding
{
    const utf_marker = 'encoding="utf-8"';
    const file_types = 'xml|xsl|xhtml|html';
    const skip_dirs  = '.|..|.svn|CVS';
    // Can't use real name since PhpEd will overwrite this file on quit.
    const enc_file   = 'c:/program files/nusphere/phped/fileenc.cfg.new';

    private $files = array();

    public function run($start_dir)
    {
        if (is_dir($start_dir)) {
            $this->scan($start_dir);
            $this->saveinfo();
        }
    }

    private function scan($dir)
    {
        $dir_r = opendir($dir);
        while (($file_name = readdir($dir_r)) !== false) {
            $path = realpath("$dir/$file_name");
            if (is_dir($path)) {
                if (strstr(self::skip_dirs, $file_name) === false) $this->scan($path);
            } elseif (preg_match('/.*(\.' . self::file_types . ')$/i', $file_name) !== false) {
                $file_r = fopen($path, 'r');
                $test_contents = fread($file_r, 200);
                if (preg_match('/' . self::utf_marker . '/i', $test_contents)) $this->files[] = $path;
                fclose($file_r);
            }
        }
        closedir($dir_r);
    }

    private function saveinfo()
    {
        $out_r = fopen(self::enc_file, 'w');
        fprintf($out_r, '<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?><root><PrefEnc><count>%d</count>', count($this->files));
        for ($i = 0; $i < count($this->files); $i++) {
            fprintf($out_r, '<_%d><File>%s</File><Encoding>UTF-8</Encoding></_%d>', $i, $this->files[$i], $i);
        }
        fprintf($out_r, '</PrefEnc></root>');
        fclose($out_r);
    }
}


$argv = $_SERVER['argv'];
$se = new SetEncoding;
$se->run($argv[1]);

?>
View user's profileFind all posts by svenaxSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8344
Reply with quote
Thanks for your great work.

I'd only recommend to replace @php@ -n -f c:\thepath\thename.php c:\webroot with
@php@ -n -f c:\thepath\thename.php "@fname@"

@fname@ is always expanded to full file name or directory name depending on what you right-clicked on.

also, I'd use @php5@ to make sure php is of version 5 as the script can't work under php4.
View user's profileFind all posts by dmitriSend private messageVisit poster's website
Automatically setting the encoding for many files
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic