Maison > développement back-end > tutoriel php > PHP中文竖排转换实现方法,_PHP教程

PHP中文竖排转换实现方法,_PHP教程

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Libérer: 2016-07-12 09:06:43
original
1325 Les gens l'ont consulté

PHP中文竖排转换实现方法,

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

效果图

index.php内容

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<&#63;php

include('ccw.inc.php');

  

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

 $ccw = new CCW;

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

}

&#63;>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<form method="post" charset="utf-8">

 <p><&#63;php echo $converd &#63;></p>

 <p><textarea name="string" cols="50" rows="10"></textarea></p>

 <p><input type="submit" /></p>

</form>

Copier après la connexion

ccw.inc.php文件内容:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

<&#63;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', '(' => '︵', ')' => '︶', '[' => '︻', ']' => '︼',

 '{' => '︷', '}' => '︸', '<' => '︽', '>' => '︾', '*' => '*', '&' => '&',

 '^' => '︿', '%' => '%', '$' => '$', '#' => '#', '@' => '@', '!' => '!',

 '~' => '~', '`' => '`', '+' => '+', '-' => '-', '=' => '=', '_' => '_',

 '|' => '|', '\\' =>'\', '\'' =>''', '"' => '"', ';' => ';', ':' => ':',

 '.' => '.', ',' => ',', '&#63;' => '?', '/' => '/', ' ' => ' ', '(' => '︵',

 ')' => '︶', '【' => '︻', '】' => '︼', '《' => '︽', '》' => '︾'

 );

  

 public $height = 10; // 默认竖排高度

  

 /**

 * 转换文字到竖排

 *

 * @return string

 */

 function convert($original, $height = null) {

 $original = preg_replace('/\s/', '', $original); // 去除多余的空格等

 $strarr = $this->mbStringToArray($original); // 分解成数组

 $height = $height &#63; 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]) &#63; $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]) &#63; $result[$i][$j] : $this->BLANK;

 }

 $output .= $this->SEPARATOR;

 $output .= "\n";

 }

  

 return $output."

Copier après la connexion
"; } /** * 转换字符串至数组 */ 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中文竖排转换的实现方法,希望对大家的学习有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1063900.htmlTechArticlePHP中文竖排转换实现方法, PHP中文竖排转换程序,文本框输入文字,转换后会竖排文字。 效果图 index.php内容 php include('ccw.inc.php'); if (iss...
Étiquettes associées:
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers numéros
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal