您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
利用PHP怎么对竖排中文进行转换?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
index.php内容
<?php include('ccw.inc.php'); if (isset($_POST['string'])){ $ccw = new CCW; $converd = $ccw->convert($_POST['string']); } ?> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <form method="post" charset="utf-8"> <p><?php echo $converd ?></p> <p><textarea name="string" cols="50" rows="10"></textarea></p> <p><input type="submit" /></p> </form>
ccw.inc.php文件内容:
<?php /** * 转换中文字符串至古文排版 */ class CCW { protected $SEPARATOR = '┆'; protected $BLANK = ' '; protected $CHARLIST = array( '0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', 'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i', 'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n', 'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's', 't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x', 'y' => 'y', 'z' => 'z', 'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E', 'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J', 'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O', 'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T', 'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y', 'Z' => 'Z', '(' => '︵', ')' => '︶', '[' => '︻', ']' => '︼', '{' => '︷', '}' => '︸', '<' => '︽', '>' => '︾', '*' => '*', '&' => '&', '^' => '︿', '%' => '%', '$' => '$', '#' => '#', '@' => '@', '!' => '!', '~' => '~', '`' => '`', '+' => '+', '-' => '-', '=' => '=', '_' => '_', '|' => '|', '\\' =>'\', '\'' =>''', '"' => '"', ';' => ';', ':' => ':', '.' => '.', ',' => ',', '?' => '?', '/' => '/', ' ' => ' ', '(' => '︵', ')' => '︶', '【' => '︻', '】' => '︼', '《' => '︽', '》' => '︾' ); public $height = 10; // 默认竖排高度 /** * 转换文字到竖排 * * @return string */ function convert($original, $height = null) { $original = preg_replace('/\s/', '', $original); // 去除多余的空格等 $strarr = $this->mbStringToArray($original); // 分解成数组 $height = $height ? intval($height) : $this->height; $total = sizeof($strarr); $width = ceil($total / $height); // 分割中文字符 $result = array(); for ($i = 0, $tmp = array(); $i < $total; $i++) { $c = $strarr[$i]; // 格式化当前字符 $tmp[] = isset($this->CHARLIST[$c]) ? $this->CHARLIST[$c] : $c; if (sizeof($tmp) == $height) { $result[] = $tmp; $tmp = array(); } } // 如果还有剩余的字符 if (sizeof($tmp)) { $result[] = $tmp; } // 开始输出 $output = "<pre>"; for($j = 0; $j < $height; $j++) { for ($i = $width - 1; $i >= 0; $i--) { $output .= $this->SEPARATOR; $output .= isset($result[$i][$j]) ? $result[$i][$j] : $this->BLANK; } $output .= $this->SEPARATOR; $output .= "\n"; } return $output."</pre>"; } /** * 转换字符串至数组 */ private function mbStringToArray ($string, $encoding = 'utf-8') { while ($strlen = mb_strlen($string)) { $array[] = mb_substr($string, 0, 1, $encoding); $string = mb_substr($string, 1, $strlen, $encoding); } return $array; } } ?>
关于利用PHP怎么对竖排中文进行转换问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。