首頁 >後端開發 >php教程 >PHP subistr 截取字串無碼

PHP subistr 截取字串無碼

WBOY
WBOY原創
2016-07-25 09:08:491459瀏覽

更新时间:20120102

  1. /**
  2. * [WebodMin] (C)2010-2099 Blank Inc.
  3. * This is NOT a freeware, use is subject to license terms
  4. *
  5. **/
  6. function subistr($string, $length, $dot = ' ...') {
  7. global $charset;
  8. if(strlen($string) <= $length) {
  9. return $string;
  10. }
  11. $s = '';
  12. if(strtolower($charset) == 'utf-8') {
  13. $n = $tn = $noc = 0;
  14. while($n < strlen($string)) {
  15. $t = ord($string[$n]);
  16. if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
  17. $tn = 1; $n++; $noc++;
  18. } elseif(194 <= $t && $t <= 223) {
  19. $tn = 2; $n += 2; $noc += 2;
  20. } elseif(224 <= $t && $t <= 239) {
  21. $tn = 3; $n += 3; $noc += 2;
  22. } elseif(240 <= $t && $t <= 247) {
  23. $tn = 4; $n += 4; $noc += 2;
  24. } elseif(248 <= $t && $t <= 251) {
  25. $tn = 5; $n += 5; $noc += 2;
  26. } elseif($t == 252 || $t == 253) {
  27. $tn = 6; $n += 6; $noc += 2;
  28. } else {
  29. $n++;
  30. }
  31. if($noc >= $length) {
  32. break;
  33. }
  34. }
  35. if($noc > $length) {
  36. $n -= $tn;
  37. }
  38. $s = substr($string, 0, $n);
  39. } else {
  40. for($i = 0; $i < $length; $i++) {
  41. $s .= ord($string[$i]) >= 127 ? $string[$i].$string[++$i] : $string[$i];
  42. }
  43. }
  44. return $s.$dot;
  45. }
复制代码


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:wecis 2.0 RC 版下一篇:wecis 2.0 RC 版