[MANUAL] English:
[zend.git] / documentation / manual / zh / tools / convEncoding.php
blob42e5449003c540542a41e782ed870475494bfcd9
1 <?php
2 function toUtf8($ar)
4 $c = '';
5 foreach($ar as $val)
7 $val = intval(substr($val,2),16);
8 if($val < 0x7F)
9 { // 0000-007F
10 $c .= chr($val);
12 elseif($val < 0x800)
13 { // 0080-0800
14 $c .= chr(0xC0 | ($val / 64));
15 $c .= chr(0x80 | ($val % 64));
17 else
18 { // 0800-FFFF
19 $c .= chr(0xE0 | (($val / 64) / 64));
20 $c .= chr(0x80 | (($val / 64) % 64));
21 $c .= chr(0x80 | ($val % 64));
24 return $c;
26 function uniDecode($str, $charcode="UTF-8")
28 $text = preg_replace_callback("/%u[0-9A-Za-z]{4}/", 'toUtf8', $str);
29 return mb_convert_encoding($text, $charcode, 'utf-8');
32 function cp($m)
34 return iconv('UTF-8', 'GBK', uniDecode('%u' . dechex($m[1])));
37 function c($str)
39 return preg_replace_callback('/&#([0-9]+);/', 'cp', $str);
42 if (isset($_GET['fileName']))
44 $fileName = $_GET['fileName'];
46 else if(isset($argv[1]))
48 $fileName = $argv[1];
50 else
52 echo "Usage: In the command line: php scriptName fileName or on the web http://foobar/scriptName.php?fileName=foobar";
55 if (isset($_GET['output']))
57 $output = $_GET['output'];
59 elseif(isset($argv[2]))
61 $output = $argv[2];
63 else
65 $p = pathinfo($fileName);
66 $output = $p['basename'];
69 $file = file_get_contents($fileName);
70 $file = c($file);
71 file_put_contents($output, $file);