国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > PHP > 正文

php中英文字符串截取函數(shù)(包括html)

2024-09-04 11:48:36
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這里提供一款支持中文漢字與英文混合在一起的截取功能,包括對(duì)html標(biāo)簽等進(jìn)來(lái)處理,下面我們來(lái)看看這款截取函數(shù)吧.

中英文字符串截取函數(shù)(包括html),實(shí)例代碼如下:

  1. function get_word($string$length$dot = '..',$charset='gbk') {  
  2. if(strlen($string) <= $length) {  
  3. return $string;  
  4. }  
  5. $string = str_replace(array(' ',' ''&''"''<''>'), array('','','&''"''<''>'), $string);  
  6. $strcut = '';  
  7. if(strtolower($charset) == 'utf-8') {  
  8. $n = $tn = $noc = 0;  
  9. while($n < strlen($string)) {  
  10. $t = ord($string[$n]);  
  11. if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {  
  12. $tn = 1; $n++; $noc++;  
  13. elseif(194 <= $t && $t <= 223) {  
  14. $tn = 2; $n += 2; $noc += 2;  
  15. elseif(224 <= $t && $t < 239) {  
  16. $tn = 3; $n += 3; $noc += 2;  
  17. elseif(240 <= $t && $t <= 247) {  
  18. $tn = 4; $n += 4; $noc += 2;  
  19. elseif(248 <= $t && $t <= 251) {  
  20. $tn = 5; $n += 5; $noc += 2;  
  21. elseif($t == 252 || $t == 253) {  
  22. $tn = 6; $n += 6; $noc += 2;  
  23. else {  
  24. $n++;  
  25. }  
  26. if($noc >= $length) {  
  27. break;  
  28. }  
  29. }  
  30. if($noc > $length) {  
  31. $n -= $tn;  
  32. }  
  33. $strcut = substr($string, 0, $n);  
  34. else {  
  35. for($i = 0; $i < $length$i++) {  
  36. $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];  
  37. }  
  38. }  
  39. return $strcut.$dot;  
  40. }  
  41. $str = "歡迎 visit 簡(jiǎn)明 nowamagic";  
  42. $str_result = get_word($str, 12);  
  43. echo $str_result

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 策勒县| 江阴市| 建始县| 团风县| 丽江市| 民乐县| 施秉县| 罗平县| 泾川县| 铁岭县| 成都市| 定南县| 娱乐| 永泰县| 延寿县| 肃南| 渑池县| 墨玉县| 苏尼特左旗| 延边| 深圳市| 千阳县| 专栏| 绥中县| 留坝县| 乌兰浩特市| 若尔盖县| 颍上县| 诸城市| 巴彦淖尔市| 广宗县| 武安市| 韶山市| 永丰县| 嘉祥县| 江永县| 荆州市| 乐陵市| 柘荣县| 张家港市| 卫辉市|