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>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/xiazai/code/9834">
<img src="https://img.php.cn/upload/webcode/000/000/016/176007960593892.png" alt="大米商城damishop">
</a>
<div class="aritcle_card_info">
<a href="/xiazai/code/9834">大米商城damishop</a>
<p>damishop介绍 大米外贸商城系统 简称damishop 完全开源版,只需做一种语言一键开启全球133中语言自动翻译功能,价格实现自动汇率转换,集成微信支付宝 paypal以及国外主流支付方式,自带文章博客系统,首创支持可视化编辑。 软件架构 基于MVC+语言包模式,增加控制台,API导入产品方便对接其他系统(带json示例数据)。 使用要求 </p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="大米商城damishop">
<span>0</span>
</div>
</div>
<a href="/xiazai/code/9834" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="大米商城damishop">
</a>
</div>
<p><input type="submit" /></p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免费学习笔记(深入)</a>”;</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 class="brush:php;toolbar:false;">";
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 .= "
";
}
return $output."以上就是php中文竖排转换的实现方法,希望对大家的学习有所帮助。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号