php中截取中文字符串

WBOY
Freigeben: 2016-07-25 08:42:12
Original
952 Leute haben es durchsucht
1. 截取GB2312中文字符串
  1. //截取中文字符串
  2. function mysubstr($str, $start, $len) {
  3. $tmpstr = "";
  4. $strlen = $start + $len;
  5. for($i = 0; $i if(ord(substr($str, $i, 1)) > 0xa0) {
  6. $tmpstr .= substr($str, $i, 2);
  7. $i++;
  8. } else
  9. $tmpstr .= substr($str, $i, 1);
  10. }
  11. return $tmpstr;
  12. }
  13. ?>
复制代码
2. 截取utf8编码的多字节字符串
  1. //截取utf8字符串
  2. function utf8Substr($str, $from, $len)
  3. {
  4. return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.
  5. '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',
  6. '$1',$str);
  7. }
  8. ?>
复制代码
3. UTF-8、GB2312都支持的汉字截取函数
  1. /*
  2. Utf-8、gb2312都支持的汉字截取函数
  3. cut_str(字符串, 截取长度, 开始长度, 编码);
  4. 编码默认为 utf-8
  5. 开始长度默认为 0
  6. */
  7. function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
  8. {
  9. if($code == 'UTF-8')
  10. {
  11. $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
  12. preg_match_all($pa, $string, $t_string);
  13. if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
  14. return join('', array_slice($t_string[0], $start, $sublen));
  15. }
  16. else
  17. {
  18. $start = $start*2;
  19. $sublen = $sublen*2;
  20. $strlen = strlen($string);
  21. $tmpstr = '';
  22. for($i=0; $i {
  23. if($i>=$start && $i {
  24. if(ord(substr($string, $i, 1))>129)
  25. {
  26. $tmpstr.= substr($string, $i, 2);
  27. }
  28. else
  29. {
  30. $tmpstr.= substr($string, $i, 1);
  31. }
  32. }
  33. if(ord(substr($string, $i, 1))>129) $i++;
  34. }
  35. if(strlen($tmpstr) return $tmpstr;
  36. }
  37. }
  38. $str = "abcd需要截取的字符串";
  39. echo cut_str($str, 8, 0, 'gb2312');
  40. ?>
复制代码
4. BugFree 的字符截取函数
  1. /**
  2. * @package BugFree
  3. * @version $Id: FunctionsMain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss Exp $
  4. *
  5. *
  6. * Return part of a string(Enhance the function substr())
  7. *
  8. * @author Chunsheng Wang
  9. * @param string $String the string to cut.
  10. * @param int $Length the length of returned string.
  11. * @param booble $Append whether append "...": false|true
  12. * @return string the cutted string.
  13. */
  14. function sysSubStr($String,$Length,$Append = false)
  15. {
  16. if (strlen($String) {
  17. return $String;
  18. }
  19. else
  20. {
  21. $I = 0;
  22. while ($I {
  23. $StringTMP = substr($String,$I,1);
  24. if ( ord($StringTMP) >=224 )
  25. {
  26. $StringTMP = substr($String,$I,3);
  27. $I = $I + 3;
  28. }
  29. elseif( ord($StringTMP) >=192 )
  30. {
  31. $StringTMP = substr($String,$I,2);
  32. $I = $I + 2;
  33. }
  34. else
  35. {
  36. $I = $I + 1;
  37. }
  38. $StringLast[] = $StringTMP;
  39. }
  40. $StringLast = implode("",$StringLast);
  41. if($Append)
  42. {
  43. $StringLast .= "...";
  44. }
  45. return $StringLast;
  46. }
  47. }
  48. $String = "17test.info 走在中国自动化测试的前沿";
  49. $Length = "18";
  50. $Append = false;
  51. echo sysSubStr($String,$Length,$Append);
  52. ?>
复制代码

php


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!