PHP封装拼音类工具来源
include 'pinyin.class.php';
$dir = 'D:\Temp';
read_and_turn($dir);
function read_and_turn($dir) {
if (!is_dir($dir)) {
return false;
}
$handler = opendir($dir);
if ($handler) {
while (($file = readdir($handler)) !== false) {
$temp = $dir. DIRECTORY_SEPARATOR . $file;
if ($file != '.' && $file != '..') {
if (preg_match("/([\x81-\xfe][\x40-\xfe])/", $file)) {
if (stripos($file, '.')) {
$pathinfo = pathinfo($file);
$filename = $pathinfo['filename'];
$pinyin = pinyin::get($filename, 0, '', '-');
$suff = $pathinfo['extension'];
$newfilename = $dir . DIRECTORY_SEPARATOR . $pinyin . '.' . $suff;
} else {
$filename = $file;
$pinyin = pinyin::get($filename, 0, '', '-');
$newfilename = $dir . DIRECTORY_SEPARATOR . $pinyin;
}
$oldfilename = iconv('UTF-8','GBK',$temp);
$oldfilename = $temp;
$res = rename($oldfilename, $newfilename);
if ($res) {
echo 'ok<br>';
} else {
echo 'fail<br>';
}
$temp = $newfilename;
}
if (is_dir($temp)) {
echo '目录:'.$temp.'<br>';
read_and_turn($temp);
}
}
}
closedir($handler);
}
}
|