欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

四个php中文字符串截取函数

程序员文章站 2022-05-01 17:30:12
...
  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. @http://bbs.it-home.org
  7. */
  8. function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
  9. {
  10. if($code == 'UTF-8')
  11. {
  12. $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]/";
  13. preg_match_all($pa, $string, $t_string);
  14. if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
  15. return join('', array_slice($t_string[0], $start, $sublen));
  16. }
  17. else
  18. {
  19. $start = $start*2;
  20. $sublen = $sublen*2;
  21. $strlen = strlen($string);
  22. $tmpstr = '';
  23. for($i=0; $i{
  24. if($i>=$start && $i{
  25. if(ord(substr($string, $i, 1))>129)
  26. {
  27. $tmpstr.= substr($string, $i, 2);
  28. }
  29. else
  30. {
  31. $tmpstr.= substr($string, $i, 1);
  32. }
  33. }
  34. if(ord(substr($string, $i, 1))>129) $i++;
  35. }
  36. if(strlen($tmpstr)return $tmpstr;
  37. }
  38. }
  39. $str = "abcd需要截取的字符串";
  40. echo cut_str($str, 8, 0, 'gb2312');
  41. ?>
复制代码

4、BugFree 字符截取函数

  1. /**
  2. * @package BugFree
  3. * @version $Id: FunctionsMain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss Exp $
  4. * @http://bbs.it-home.org
  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 = "bbs.it-home.org 脚本学堂 专心为您;
  49. $Length = "18";
  50. $Append = false;
  51. echo sysSubStr($String,$Length,$Append);
  52. ?>
复制代码