PHP中文豎排轉換程式

來源:互聯網
上載者:User
本篇文章主要介紹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;  } } ?>

總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.