option('input') ?? base_path('resources/data/Unihan_IRGSources.txt'); $output = $this->option('output') ?? base_path('resources/data/unihan_strokes.php'); if (!file_exists($input)) { $this->error("找不到輸入檔案: $input"); return 1; } $handle = fopen($input, 'r'); if (!$handle) { $this->error("無法開啟檔案: $input"); return 1; } $strokes = []; while (($line = fgets($handle)) !== false) { $line = trim($line); if ($line === '' || $line[0] === '#') { continue; } if (preg_match('/^U\+([0-9A-F]+)\s+kTotalStrokes\s+(\d+)/i', $line, $matches)) { $unicodeHex = 'U+' . strtoupper($matches[1]); $strokeCount = intval($matches[2]); $strokes[$unicodeHex] = $strokeCount; } } fclose($handle); // 寫入 PHP 陣列檔 //$exported = var_export($strokes, true); //$phpCode = " $value) { $line .= var_export($key, true) . ' => ' . var_export($value, true) . ', '; $count++; if ($count % 100 === 0) { $lines[] = rtrim($line); $line = ' '; } } if (trim($line) !== '') { $lines[] = rtrim($line); } $lines[] = "];"; $lines[] = ""; $phpCode = implode("\n", $lines); file_put_contents($output, $phpCode); $this->info("✅ 轉換完成,資料已儲存至: $output"); return 0; } }