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?
<?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]);
?> |