PHP中文竖排转换实现方法


Posted in PHP onOctober 23, 2015

PHP中文竖排转换程序,文本框输入文字,转换后会竖排文字。

效果图

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中文竖排转换的实现方法,希望对大家的学习有所帮助。

PHP 相关文章推荐
如何过滤高亮显示非法字符
Oct 09 PHP
关于PHP5 Session生命周期介绍
Mar 02 PHP
paypal即时到账php实现代码
Nov 28 PHP
php file_put_contents()功能函数(集成了fopen、fwrite、fclose)
May 24 PHP
PHP 登录完成后如何跳转上一访问页面
Jan 14 PHP
PHP判断表单复选框选中状态完整例子
Jun 24 PHP
PHP魔术引号所带来的安全问题分析
Jul 15 PHP
PHP的反射类ReflectionClass、ReflectionMethod使用实例
Aug 05 PHP
Thinkphp无限级分类代码
Nov 11 PHP
PHP的压缩函数实现:gzencode、gzdeflate和gzcompress的区别
Jan 27 PHP
PHP自定义多进制的方法
Nov 03 PHP
php进程daemon化的正确实现方法
Sep 06 PHP
浅谈php7的重大新特性
Oct 23 #PHP
php数字每三位加逗号的功能函数
Oct 22 #PHP
jQuery+PHP发布的内容进行无刷新分页(Fckeditor)
Oct 22 #PHP
PHP+Mysql+jQuery查询和列表框选择操作实例讲解
Oct 22 #PHP
PHP实现无限级分类(不使用递归)
Oct 22 #PHP
PHP实现递归无限级分类
Oct 22 #PHP
php防止网站被攻击的应急代码
Oct 21 #PHP
You might like
Protoss兵种介绍
2020/03/14 星际争霸
php读取csv数据保存到数组的方法
2015/01/03 PHP
详解PHP的Yii框架中自带的前端资源包的使用
2016/03/31 PHP
简单谈谈 php 文件锁
2017/02/19 PHP
PHP基于面向对象实现的留言本功能实例
2018/04/04 PHP
js实现从右向左缓缓浮出网页浮动层广告的方法
2015/05/09 Javascript
jQuery实现多级下拉菜单jDropMenu的方法
2015/08/28 Javascript
JS显示日历和天气的方法
2016/03/01 Javascript
使用jQuery处理AJAX请求的基础学习教程
2016/05/10 Javascript
Javascript使用uploadify来实现多文件上传
2016/11/16 Javascript
vue日期组件 支持vue1.0和2.0
2017/01/09 Javascript
详解本地Node.js服务器作为api服务器的解决办法
2017/02/28 Javascript
整理关于Bootstrap导航的慕课笔记
2017/03/29 Javascript
js实现文字列表无缝滚动效果
2017/06/23 Javascript
浅谈Vue路由快照实现思路及其问题
2018/06/07 Javascript
关于layui的动态图标不显示的解决方法
2019/09/04 Javascript
layui禁用侧边导航栏点击事件的解决方法
2019/09/25 Javascript
js实现弹幕飞机效果
2020/08/27 Javascript
Python 随机生成中文验证码的实例代码
2013/03/20 Python
Python实现的井字棋(Tic Tac Toe)游戏示例
2018/01/31 Python
Python用Try语句捕获异常的实例方法
2019/06/26 Python
python requests使用socks5的例子
2019/07/25 Python
docker-py 用Python调用Docker接口的方法
2019/08/30 Python
无需压缩软件,用python帮你操作压缩包
2020/08/17 Python
python报错TypeError: ‘NoneType‘ object is not subscriptable的解决方法
2020/11/05 Python
家长评语和期望
2014/02/10 职场文书
销售团队口号大全
2014/06/06 职场文书
计算机科学技术自荐信
2014/06/12 职场文书
辞职信模板(中英文版)
2015/02/27 职场文书
关于召开会议的通知
2015/04/15 职场文书
MySQL 8.0 Online DDL快速加列的相关总结
2021/06/02 MySQL
深入理解python协程
2021/06/15 Python
解决SpringBoot跨域的三种方式
2021/06/26 Java/Android
十大必看国产动漫排名,魁拔上线,第二曾在日本播出
2022/03/18 国漫
SQL Server的存储过程与触发器以及系统函数和自定义函数
2022/04/10 SQL Server
nginx 配置指令之location使用详解
2022/05/25 Servers