用php打竖的文字_PHP中文竖排转换实现方法

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

效果图

281cd18b4ee2518310db44ebbc1ea97e.png

index.php内容

include('ccw.inc.php');

if (isset($_POST['string'])){

$ccw = new CCW;

$converd = $ccw->convert($_POST['string']);

}

?>

ccw.inc.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 = "

";

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."

";

}

/**

* 转换字符串至数组

*/

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值