3 require_once 'simplepie.inc';
5 function normalize_character_set($charset)
7 return strtolower(preg_replace('/(?:[^a-zA-Z0-9]+|([^0-9])0+)/', '\1', $charset));
10 function build_character_set_list()
12 $file = new SimplePie_File('http://www.iana.org/assignments/character-sets');
13 if (!$file->success && !($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)))
19 $data = explode("\n", $file->body);
22 foreach ($data as $line)
25 if (substr($line, 0, 5) === 'Name:')
27 // If we already have one, push it on to the array
30 for ($i = 0, $count = count($aliases); $i < $count; $i++)
32 $aliases[$i] = normalize_character_set($aliases[$i]);
34 $charsets[$preferred] = array_unique($aliases);
35 natsort($charsets[$preferred]);
38 $start = 5 + strspn($line, "\x09\x0A\x0B\xC\x0D\x20", 5);
39 $chars = strcspn($line, "\x09\x0A\x0B\xC\x0D\x20", $start);
40 $aliases = array(substr($line, $start, $chars));
41 $preferred = end($aliases);
44 elseif(substr($line, 0, 6) === 'Alias:')
46 $start = 7 + strspn($line, "\x09\x0A\x0B\xC\x0D\x20", 7);
47 $chars = strcspn($line, "\x09\x0A\x0B\xC\x0D\x20", $start);
48 $aliases[] = substr($line, $start, $chars);
50 if (end($aliases) === 'None')
54 elseif (substr($line, 7 + $chars + 1, 21) === '(preferred MIME name)')
56 $preferred = end($aliases);
61 // Compatibility replacements
63 'EUC-KR' => 'windows-949',
65 'GB_2312-80' => 'GBK',
66 'ISO-8859-1' => 'windows-1252',
67 'ISO-8859-9' => 'windows-1254',
68 'ISO-8859-11' => 'windows-874',
69 'KS_C_5601-1987' => 'windows-949',
70 'TIS-620' => 'windows-874',
71 //'US-ASCII' => 'windows-1252',
75 foreach ($compat as $real => $replace)
77 if (isset($charsets[$real]) && isset($charsets[$replace]))
79 $charsets[$replace] = array_merge($charsets[$replace], $charsets[$real]);
80 unset($charsets[$real]);
82 elseif (isset($charsets[$real]))
84 $charsets[$replace] = $charsets[$real];
85 $charsets[$replace][] = normalize_character_set($replace);
86 unset($charsets[$real]);
90 $charsets[$replace][] = normalize_character_set($real);
92 $charsets[$replace] = array_unique($charsets[$replace]);
93 natsort($charsets[$replace]);
97 uksort($charsets, 'strnatcasecmp');
99 // Check that nothing matches more than one
100 $all = call_user_func_array('array_merge', $charsets);
101 $all_count = array_count_values($all);
102 if (max($all_count) > 1)
104 echo "Duplicated charsets:\n";
105 foreach ($all_count as $charset => $count)
119 function charset($charset)
121 $normalized_charset = normalize_character_set($charset);
122 if ($charsets = build_character_set_list())
124 foreach ($charsets as $preferred => $aliases)
126 if (in_array($normalized_charset, $aliases))
139 function build_function()
141 if ($charsets = build_character_set_list())
144 function charset(\$charset)
146 // Normalization from UTS #22
147 switch (strtolower(preg_replace('/(?:[^a-zA-Z0-9]+|([^0-9])0+)/', '\\1', \$charset)))
151 foreach ($charsets as $preferred => $aliases)
153 foreach ($aliases as $alias)
155 $return .= "\t\tcase " . var_export($alias, true) . ":\n";
157 $return .= "\t\t\treturn " . var_export($preferred, true) . ";\n\n";
173 if (php_sapi_name() === 'cli' && realpath($_SERVER['argv'][0]) === __FILE__)
175 echo build_function();