当前位置:Gxlcms > PHP教程 > php针对中英文混合排版之字符串常用的函数

php针对中英文混合排版之字符串常用的函数

时间:2021-07-01 10:21:17 帮助过:8人阅读

# 判断某个位置是中文字符的左还是右半部分,或不是中文
# 返回值 -1 左 0 不是中文字符 1 右
# 用法

  1. /*
  2. $a = 'this is 中文';
  3. print is_chinese($a, 1); // 0
  4. print is_chinese($a,8); // -1
  5. print is_chinese($a,9); // 1
  6. */
  7. function is_chinese(&$str, $location) {
  8. $ch = true;
  9. $i = $location;
  10. while(ord($str[$i])>0xa0 && $i >= 0) {
  11. $ch = !$ch;
  12. $i --;
  13. }
  14. if($i != $location) {
  15. $f_str = $ch ? 1: -1;
  16. }
  17. else {
  18. $f_str = false;
  19. }
  20. return $f_str;
  21. }
  22. # 中文
  23. 字符串
  24. 倒置函数
  25. # 如果一个将一个有中文的字符串用
  26. strrev
  27. 倒过来,就会产生乱码
  28. /*
  29. print cstrrev('this is 中文'); // 文中 si siht
  30. */
  31. function cstrrev(&$str) {
  32. $long =
  33. strlen
  34. ($str);
  35. for($f_str='', $chinese=false, $i=$long-1; $i>=0; $i--) {
  36. if(ord($str[$i]) > 0xa0) {
  37. $chinese = ! $chinese;
  38. if($chinese == false) {
  39. $f_str .= $str[$i].$str[$i+1];
  40. }
  41. }
  42. else {
  43. $f_str .= $str[$i];
  44. }
  45. }
  46. return $f_str;
  47. }
  48. /* 中文
  49. 字符串截取
  50. 函数
  51. 一些中文字符串截取函数经常有一些问题,例如在一些自动换行程序中
  52. $a=“1中2”;
  53. 经两次截取后,
  54. c
  55. substr
  56. ($str,$a,0,2);
  57. csubstr($str, $a, 2,2)
  58. 由于载取位置指向“中”的右字节,可能会是这样的结果
  59. 1, 2
  60. 用本函数会产生正确的结果
  61. 1中, 2
  62. */
  63. # start 开始位置,从0开始
  64. # long = 0 则从start 一直取到字符串尾
  65. # ltor = true 时从左到右取字符,false 时到右到左取字符
  66. # $cn_len 中文字符按字节取还是字数取,如果按字数取,则一个中文当一个字节计算
  67. function csubstr(&$str, $start=0, $long=0, $ltor=true, $cn_len=2) {
  68. if($long == 0) $long = strlen($str);
  69. if($ltor == false) $str = cstrrev($str);
  70. if($cn_len == 1) {
  71. for($i=0, $fs=0; $i<$start; $fs++)
  72. $i += (ord($str[$fs]) <= 0xa0) ? 1 : 0.5;
  73. for($i=0, $fe=$fs; $i<$long; $fe++)
  74. $i += (ord($str[$fe]) <= 0xa0) ? 1 : 0.5;
  75. $long = $fe - $fs;
  76. }
  77. else {
  78. $fs = (is_chinese($str, $start) == 1) ? $start - 1 : $start;
  79. $fe = $long + $start - 1;
  80. $end = ( is_chinese($str, $fe) == -1 ) ? $fe -1 : $fe;
  81. $long = $end - $fs + 1;
  82. }
  83. $f_str = substr($str, $fs, $long);
  84. if($ltor == false) $f_str = cstrrev($f_str);
  85. return $f_str;
  86. }
  87. # 取左字符串
  88. # 当cn_len == 2 时 $long 取左边多少个字,反之则取左边多少个字节
  89. function cleft(&$str, $long, $cn_len=2) {
  90. $f_str = csubstr($str, 0, $long, true, $cn_len);
  91. return $f_str;
  92. }
  93. # 取右字符串
  94. function cright(&$str, $long, $cn_len=2) {
  95. $f_str = cstrrev($str);
  96. $f_str = csubstr($f_str, 0, $long, true, $cn_len);
  97. $f_str = cstrrev($f_str);
  98. return $f_str;
  99. }
  100. # 对含有中文字符的文章分行格式化
  101. # 再也不会发生因换行问题而产生的种种问题啦!!!
  102. # 注:文章的每一行必须用 n (chr(13))进行分行
  103. # $width 每行多少字符
  104. # $br 将 每行用什么字符当结束符
  105. function ctext_wrap(&$text, $width=60, $br="<BR>") {
  106. $lines =
  107. explode
  108. ("n",$text);
  109. $rows = count($lines);
  110. for($i=0; $i<$rows; $i++) {
  111. $len = strlen($lines[$i]);
  112. for($j=0; $j<$len; $j+=$width) {
  113. $p = $j + $width - 1;
  114. $k = 0;
  115. if($p<$len) {
  116. while(!is_chinese($lines[$i], $p) && $lines[$i][$p] != ' ' && $p>$j) {
  117. $k ++;
  118. $p --;
  119. }
  120. if($p == $j) $k = 0;
  121. }
  122. $f_str .= csubstr($lines[$i], $j, $width-$k) . $br;
  123. $j -= $k;
  124. }
  125. }
  126. return $f_str;
  127. }

以上就是php 针对中英文混合排版之字符串常用的函数的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行