Skip to content

Instantly share code, notes, and snippets.

@codemasher
Created January 30, 2023 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codemasher/91da33c44bfb48a81a6c1426bb8e4338 to your computer and use it in GitHub Desktop.
Save codemasher/91da33c44bfb48a81a6c1426bb8e4338 to your computer and use it in GitHub Desktop.
Create a table of GB2312/Hanzi in PHP
<?php
/**
* gb2312-table.php
*
* @see https://en.wikipedia.org/wiki/GB_2312
* @see http://www.herongyang.com/GB2312/Introduction-of-GB2312.html
* @see https://en.wikipedia.org/wiki/GBK_(character_encoding)#Encoding
*
* @created 30.01.2023
* @author smiley <smiley@chillerlan.net>
* @copyright 2023 smiley
* @license MIT
*/
$list = [];
for($byte1 = 0xa1; $byte1 < 0xf8; $byte1 += 0x1){
if($byte1 > 0xa9 && $byte1 < 0xb0){
continue;
}
$list[$byte1] = array_fill(0xa0, 96, null); // 0xa1 - 0xfe
for($byte2 = 0xa1; $byte2 < 0xff; $byte2++){
$list[$byte1][$byte2] = chr($byte1).chr($byte2);
}
}
// output
if(php_sapi_name() !== 'cli'){
echo '<!doctype html>
<html lang="cn">
<head>
<meta charset="UTF-8">
<title>GB2312 Hanzi Code Table</title>
</head>
<body>
';
foreach($list as $chars){
$chars = array_map(fn($chr) => ($chr !== null ? mb_convert_encoding($chr, 'UTF-8', 'GB2312') : null), $chars);
$rows = [];
foreach(array_chunk($chars, 16, true) as $row){
$rows[] = sprintf('<td>%s</td>', implode('</td><td>', $row));
}
printf("<table><tr>%s</tr></table>\n", implode('</tr><tr>', $rows));
}
echo '</body>';
}
else{
foreach($list as $chars){
$chars = array_map(fn($chr) => ($chr !== null ? mb_convert_encoding($chr, 'UTF-8', 'GB2312') : null), $chars);
$rows = [];
foreach(array_chunk($chars, 16, true) as $row){
$rows[] = implode(' ', $row);
}
printf("%s\n", implode("\n", $rows));
}
}
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment