1 <?php
2 /**
3 * Convert Chinese to Classical Chinese
4 *
5 * Copyright 2008 YiXia <yixia@e-xia.com>
6 *
7 * modified by feelinglucky<i.feelinglucky@gmail.com>
8 * form http://www.gracecode.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 */
20
21 /**
22 * 转换中文字符串至古文排版
23 */
24 class CCW {
25 protected $SEPARATOR = '┆';
26 protected $BLANK = ' ';
27 protected $CHARLIST = array(
28 '0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5',
29 '6' => '6', '7' => '7', '8' => '8', '9' => '9', 'a' => 'a', 'b' => 'b',
30 'c' => 'c', 'd' => 'd', 'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h',
31 'i' => 'i', 'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
32 'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's', 't' => 't',
33 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x', 'y' => 'y', 'z' => 'z',
34 'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E', 'F' => 'F',
35 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J', 'K' => 'K', 'L' => 'L',
36 'M' => 'M', 'N' => 'N', 'O' => 'O', 'P' => 'P', 'Q' => 'Q', 'R' => 'R',
37 'S' => 'S', 'T' => 'T', 'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X',
38 'Y' => 'Y', 'Z' => 'Z', '(' => '︵', ')' => '︶', '[' => '︻', ']' => '︼',
39 '{' => '︷', '}' => '︸', '<' => '︽', '>' => '︾', '*' => '*', '&' => '&',
40 '^' => '︿', '%' => '%', '$' => '$', '#' => '#', '@' => '@', '!' => '!',
41 '~' => '~', '`' => '`', '+' => '+', '-' => '-', '=' => '=', '_' => '_',
42 '|' => '|', '\\' =>'\', '\'' =>''', '"' => '"', ';' => ';', ':' => ':',
43 '.' => '.', ',' => ',', '?' => '?', '/' => '/', ' ' => ' ', '(' => '︵',
44 ')' => '︶', '【' => '︻', '】' => '︼', '《' => '︽', '》' => '︾'
45 );
46
47 public $height = 10; // 默认竖排高度
48
49 /**
50 * 转换文字到竖排
51 *
52 * @return string
53 */
54 function convert($original, $height = null) {
55 $original = preg_replace('/\s/', '', $original); // 去除多余的空格等
56 $strarr = $this->mbStringToArray($original); // 分解成数组
57 $height = $height ? intval($height) : $this->height;
58 $total = sizeof($strarr);
59 $width = ceil($total / $height);
60
61 // 分割中文字符
62 $result = array();
63 for ($i = 0, $tmp = array(); $i < $total; $i++) {
64 $c = $strarr[$i]; // 格式化当前字符
65 $tmp[] = isset($this->CHARLIST[$c]) ? $this->CHARLIST[$c] : $c;
66 if (sizeof($tmp) == $height) {
67 $result[] = $tmp;
68 $tmp = array();
69 }
70 }
71
72 // 如果还有剩余的字符
73 if (sizeof($tmp)) {
74 $result[] = $tmp;
75 }
76
77 // 开始输出
78 $output = "<pre>";
79 for($j = 0; $j < $height; $j++) {
80 for ($i = $width - 1; $i >= 0; $i--) {
81 $output .= $this->SEPARATOR;
82 $output .= isset($result[$i][$j]) ? $result[$i][$j] : $this->BLANK;
83 }
84 $output .= $this->SEPARATOR;
85 $output .= "\n";
86 }
87
88 return $output."</pre>";
89 }
90
91
92 /**
93 * 转换字符串至数组
94 */
95 private function mbStringToArray ($string, $encoding = 'utf-8') {
96 while ($strlen = mb_strlen($string)) {
97 $array[] = mb_substr($string, 0, 1, $encoding);
98 $string = mb_substr($string, 1, $strlen, $encoding);
99 }
100
101 return $array;
102 }
103 }
104 ?>