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 相关文章推荐
PHP 和 MySQL 开发的 8 个技巧
Oct 09 PHP
php一句话cmdshell新型 (非一句话木马)
Apr 18 PHP
php读取xml实例代码
Jan 28 PHP
PHP中基本符号及使用方法
Mar 23 PHP
PHP+Mysql+jQuery实现动态展示信息
Oct 08 PHP
PHP中cookie和session的区别实例分析
Aug 28 PHP
PHP实现货币换算的方法
Nov 29 PHP
php中数据库连接方式pdo和mysqli对比分析
Feb 25 PHP
PHP SPL标准库之文件操作(SplFileInfo和SplFileObject)实例
May 11 PHP
PHP实现的蚂蚁爬杆路径算法代码
Dec 03 PHP
浅谈PHP检查数组中是否存在某个值 in_array 函数
Jun 13 PHP
php使用flock阻塞写入文件和非阻塞写入文件的实例讲解
Jul 10 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
php实现字符串首字母转换成大写的方法
2015/03/17 PHP
PHP+原生态ajax实现的省市联动功能详解
2017/08/15 PHP
PHP读取目录树的实现方法分析
2019/03/22 PHP
jQuery 行级解析读取XML文件(附源码)
2009/10/12 Javascript
jQuery 常见开发使用技巧总结
2009/12/26 Javascript
JS request函数 用来获取url参数
2010/05/17 Javascript
jquery删除指定的html标签并保留标签内文本内容的方法
2014/04/02 Javascript
jquery实现动态操作select选中
2015/02/11 Javascript
动态加载jQuery的两种方法实例分析
2015/07/17 Javascript
js实现简单计算器
2015/11/22 Javascript
JavaScript编程中实现对象封装特性的实例讲解
2016/06/24 Javascript
Angularjs上传文件组件flowjs功能
2017/08/07 Javascript
安装Node.js并启动本地服务的操作教程
2018/05/12 Javascript
Vue多选列表组件深入详解
2021/03/02 Vue.js
python网络编程学习笔记(五):socket的一些补充
2014/06/09 Python
Python中用于返回绝对值的abs()方法
2015/05/14 Python
python学习之编写查询ip程序
2016/02/27 Python
asyncio 的 coroutine对象 与 Future对象使用指南
2016/09/11 Python
python Celery定时任务的示例
2018/03/13 Python
python3结合openpyxl库实现excel操作的实例代码
2018/09/11 Python
Python异常模块traceback用法实例分析
2019/10/22 Python
Pycharm IDE的安装和使用教程详解
2020/04/30 Python
OPPO手机官方商城:中国手机市场出货量第一品牌
2017/10/18 全球购物
BookOutlet加拿大:在网上书店购买廉价折扣图书和小说
2018/10/05 全球购物
乌克兰珠宝大卖场:Zlato.ua
2020/09/27 全球购物
英语专业个人求职自荐信
2013/09/21 职场文书
人事文员岗位职责
2014/02/16 职场文书
财产保全担保书范文
2014/04/01 职场文书
活动总结报告怎么写
2014/07/03 职场文书
搞笑的获奖感言
2014/08/16 职场文书
公司委托书范本5篇
2014/09/20 职场文书
大二学生自我检讨书
2014/10/23 职场文书
大学生入党自荐书
2015/03/05 职场文书
团员自我评价范文
2015/03/10 职场文书
大学生团支书竞选稿
2015/11/21 职场文书
解析目标检测之IoU
2021/06/26 Python