1. 截取GB2312中文字符串

  1. <?php
  2. header("content-type:text/html;charset=gb2312");
  3. // echo "aaaa";
  4. //截取中文字符串
  5. ### 1. 截取GB2312中文字符串
  6. function mysubstr($str,$start,$len){
  7. $tmpstr = "";
  8. $strlen = $start + $len;
  9. for($i = 0; $i < $strlen; $i++) {
  10. if(ord(substr($str, $i, 1)) > 0xa0) {
  11. $tmpstr .= substr($str, $i, 2);
  12. $i++;
  13. } else
  14. $tmpstr .= substr($str, $i, 1);
  15. }
  16. return $tmpstr;
  17. }
  18.  
  19. $con = "123电话巴萨的sasaS";
  20.  
  21. echo mysubstr($con,4,5);
  22.  
  23. ?>

2. 截取utf8编码的多字节字符串

  1. <?php
  2. header("content-type:text/html;charset=utf-8");
  3. //截取utf8字符串
  4. function utf8Substr($str, $from, $len)
  5. {
  6. return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.
  7. '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',
  8. '$1',$str);
  9. }
  10.  
  11. $con = '123得到的ss第三32342代a12';
  12.  
  13. echo utf8Substr($con,2,8);
  14.  
  15. ?>

3. UTF-8、GB2312都支持的汉字截取函数

  1. <?php
  2. /*
  3. Utf-8、gb2312都支持的汉字截取函数
  4. cut_str(字符串, 截取长度, 开始长度, 编码);
  5. 编码默认为 utf-8
  6. 开始长度默认为 0
  7. */
  8.  
  9. function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
  10. {
  11. if($code == 'UTF-8')
  12. {
  13. $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]/";
  14. preg_match_all($pa, $string, $t_string);
  15.  
  16. if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
  17. return join('', array_slice($t_string[0], $start, $sublen));
  18. }
  19. else
  20. {
  21. $start = $start*2;
  22. $sublen = $sublen*2;
  23. $strlen = strlen($string);
  24. $tmpstr = '';
  25.  
  26. for($i=0; $i< $strlen; $i++)
  27. {
  28. if($i>=$start && $i< ($start+$sublen))
  29. {
  30. if(ord(substr($string, $i, 1))>129)
  31. {
  32. $tmpstr.= substr($string, $i, 2);
  33. }
  34. else
  35. {
  36. $tmpstr.= substr($string, $i, 1);
  37. }
  38. }
  39. if(ord(substr($string, $i, 1))>129) $i++;
  40. }
  41. if(strlen($tmpstr)< $strlen ) $tmpstr.= "...";
  42. return $tmpstr;
  43. }
  44. }
  45.  
  46. $str = "abcd需要截取的字符串";
  47. echo cut_str($str, 8, 0, 'gb2312');
  48. ?>

4. BugFree 的字符截取函数

  1. < ?php
  2. /**
  3. * @package BugFree
  4. * @version $Id: FunctionsMain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss Exp $
  5. *
  6. *
  7. * Return part of a string(Enhance the function substr())
  8. *
  9. * @author Chunsheng Wang <wwccss@263.net>
  10. * @param string $String the string to cut.
  11. * @param int $Length the length of returned string.
  12. * @param booble $Append whether append "...": false|true
  13. * @return string the cutted string.
  14. */
  15. function sysSubStr($String,$Length,$Append = false)
  16. {
  17. if (strlen($String) < = $Length )
  18. {
  19. return $String;
  20. }
  21. else
  22. {
  23. $I = 0;
  24. while ($I < $Length)
  25. {
  26. $StringTMP = substr($String,$I,1);
  27. if ( ord($StringTMP) >=224 )
  28. {
  29. $StringTMP = substr($String,$I,3);
  30. $I = $I + 3;
  31. }
  32. elseif( ord($StringTMP) >=192 )
  33. {
  34. $StringTMP = substr($String,$I,2);
  35. $I = $I + 2;
  36. }
  37. else
  38. {
  39. $I = $I + 1;
  40. }
  41. $StringLast[] = $StringTMP;
  42. }
  43. $StringLast = implode("",$StringLast);
  44. if($Append)
  45. {
  46. $StringLast .= "...";
  47. }
  48. return $StringLast;
  49. }
  50. }
  51.  
  52. $String = "17test.info 走在中国自动化测试的前沿";
  53. $Length = "18";
  54. $Append = false;
  55. echo sysSubStr($String,$Length,$Append);
  56. ?>

PHP字符串截取操作大全的更多相关文章

  1. C#中字符串的操作大全

    一.C#中字符串的建立过程 例如定义变量 strT="Welcome to "; strT+="www.cuit.edu.cn"; 程序首先创建一个System ...

  2. javascript中字符串常用操作总结、JS字符串操作大全

    字符串的操作在js中非常频繁,也非常重要.以往看完书之后都能记得非常清楚,但稍微隔一段时间不用,便会忘得差不多,记性不好是硬伤啊...今天就对字符串的一些常用操作做个整理,一者加深印象,二者方便今后温 ...

  3. JS字符串操作大全

    String对象属性 (1) length属性 length算是字符串中非常常用的一个属性了,它的功能是获取字符串的长度.当然需要注意的是js中的中文每个汉字也只代表一个字符,这里可能跟其他语言有些不 ...

  4. js--javascript中字符串常用操作总结、JS字符串操作大全

    字符串的操作在js中非常频繁,也非常重要.以往看完书之后都能记得非常清楚,但稍微隔一段时间不用,便会忘得差不多,记性不好是硬伤啊...今天就对字符串的一些常用操作做个整理,一者加深印象,二者方便今后温 ...

  5. shell字符串操作之cut---实现字符串截取

    shell中(字符串截取) cut是以每一行为一个处理对象的,这种机制和sed是一样的.(关于sed的入门文章将在近期发布) 2 cut一般以什么为依据呢? 也就是说,我怎么告诉cut我想定位到的剪切 ...

  6. linux的string操作(字符串截取,长度计算)

    按指定的字符串截取 1.第一种方法: ${varible##*string} 从左向右截取最后一个string后的字符串 ${varible#*string}从左向右截取第一个string后的字符串 ...

  7. C#字符串操作大全

    ===============================字符串基本操作================================ 一.C#中字符串的建立过程 例如定义变量 strT=&qu ...

  8. web 前端 常见操作 将时间戳转成日期格式 字符串截取 使用mui制作选项卡

    1.将时间戳转成日期格式: //第一种 function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString( ...

  9. js-DOM ~ 05. Date日期的相关操作、string、查字符串的位置、给索引查字符、字符串截取slice/substr/substring、去除空格、替换、大小写、Math函数、事件绑定、this

    内置对象:  语言自带的对象/提供了常用的.基本的功能 打印数组和字符串不用for... in   /   打印josn的时候采用for...in Date 获取当前事件:   var date = ...

随机推荐

  1. INV*更新物料信息

    物料 PROCEDURE update_item(p_init_msg_list IN VARCHAR2 DEFAULT fnd_api.g_false, x_return_status OUT NO ...

  2. 【php写日志】php将日志写入文件

    php 写内容到文件,把日志写到log文件 <?php header("Content-type: text/html; charset=utf-8"); /******** ...

  3. android中文字中间有超链接的实现方法

      1.XML里写: <resources> <string name="ACCOUNT_REGISTER_PROMPT_AGREEMENT">点击注册,表 ...

  4. 转 Android开发学习笔记:浅谈WebView

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://liangruijun.blog.51cto.com/3061169/647456 ...

  5. 使用 C# 开发智能手机软件:推箱子(十八)

    这是"使用 C# 开发智能手机软件:推箱子" 系列文章的第十八篇.在这篇文章中.介绍 Window/SelectLevelDlg.cs 源程序文件. 这个源程序文件包括 Selec ...

  6. Struts2源码阅读(一)_Struts2框架流程概述

    1. Struts2架构图  当外部的httpservletrequest到来时 ,初始到了servlet容器(所以虽然Servlet和Action是解耦合的,但是Action依旧能够通过httpse ...

  7. lo4j 日志级别

    日志记录器(Logger)的行为是分等级的.如下表所示: 分为OFF.FATAL.ERROR.WARN.INFO.DEBUG.TRACE.ALL或者您定义的级别.Log4j建议只使用四个级别,优先级从 ...

  8. 删除outlook配置信息

    1.输入“Win+R”组合键,在弹出的窗口中输入:control,打开控制面板 2.找到“邮件”选项,并单击 3.在弹出的窗口中,单击“显示配置文件”选项,删除配置文件夹,OK.

  9. Eclipse “cannot be resolved to a type” error

    引言:     eclipse新导入的项目经常可以看到"XX cannot be resolved to a type"的报错信息.本文将做以简单总结. 正文:     (1)jd ...

  10. 【pywin32总结】

    #下面是必备的#注意!所有方法后面都要加括号()!!! import win32com from win32com.client import Dispatch,constants w = win32 ...