PHP字符串截取操作大全
1. 截取GB2312中文字符串
- <?php
- header("content-type:text/html;charset=gb2312");
- // echo "aaaa";
- //截取中文字符串
- ### 1. 截取GB2312中文字符串
- function mysubstr($str,$start,$len){
- $tmpstr = "";
- $strlen = $start + $len;
- for($i = 0; $i < $strlen; $i++) {
- if(ord(substr($str, $i, 1)) > 0xa0) {
- $tmpstr .= substr($str, $i, 2);
- $i++;
- } else
- $tmpstr .= substr($str, $i, 1);
- }
- return $tmpstr;
- }
- $con = "123电话巴萨的sasaS";
- echo mysubstr($con,4,5);
- ?>
2. 截取utf8编码的多字节字符串
- <?php
- header("content-type:text/html;charset=utf-8");
- //截取utf8字符串
- function utf8Substr($str, $from, $len)
- {
- return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.
- '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',
- '$1',$str);
- }
- $con = '123得到的ss第三32342代a12';
- echo utf8Substr($con,2,8);
- ?>
3. UTF-8、GB2312都支持的汉字截取函数
- <?php
- /*
- Utf-8、gb2312都支持的汉字截取函数
- cut_str(字符串, 截取长度, 开始长度, 编码);
- 编码默认为 utf-8
- 开始长度默认为 0
- */
- function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
- {
- if($code == 'UTF-8')
- {
- $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]/";- preg_match_all($pa, $string, $t_string);
- if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
- return join('', array_slice($t_string[0], $start, $sublen));
- }
- else
- {
- $start = $start*2;
- $sublen = $sublen*2;
- $strlen = strlen($string);
- $tmpstr = '';
- for($i=0; $i< $strlen; $i++)
- {
- if($i>=$start && $i< ($start+$sublen))
- {
- if(ord(substr($string, $i, 1))>129)
- {
- $tmpstr.= substr($string, $i, 2);
- }
- else
- {
- $tmpstr.= substr($string, $i, 1);
- }
- }
- if(ord(substr($string, $i, 1))>129) $i++;
- }
- if(strlen($tmpstr)< $strlen ) $tmpstr.= "...";
- return $tmpstr;
- }
- }
- $str = "abcd需要截取的字符串";
- echo cut_str($str, 8, 0, 'gb2312');
- ?>
4. BugFree 的字符截取函数
- < ?php
- /**
- * @package BugFree
- * @version $Id: FunctionsMain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss Exp $
- *
- *
- * Return part of a string(Enhance the function substr())
- *
- * @author Chunsheng Wang <wwccss@263.net>
- * @param string $String the string to cut.
- * @param int $Length the length of returned string.
- * @param booble $Append whether append "...": false|true
- * @return string the cutted string.
- */
- function sysSubStr($String,$Length,$Append = false)
- {
- if (strlen($String) < = $Length )
- {
- return $String;
- }
- else
- {
- $I = 0;
- while ($I < $Length)
- {
- $StringTMP = substr($String,$I,1);
- if ( ord($StringTMP) >=224 )
- {
- $StringTMP = substr($String,$I,3);
- $I = $I + 3;
- }
- elseif( ord($StringTMP) >=192 )
- {
- $StringTMP = substr($String,$I,2);
- $I = $I + 2;
- }
- else
- {
- $I = $I + 1;
- }
- $StringLast[] = $StringTMP;
- }
- $StringLast = implode("",$StringLast);
- if($Append)
- {
- $StringLast .= "...";
- }
- return $StringLast;
- }
- }
- $String = "17test.info 走在中国自动化测试的前沿";
- $Length = "18";
- $Append = false;
- echo sysSubStr($String,$Length,$Append);
- ?>
PHP字符串截取操作大全的更多相关文章
- C#中字符串的操作大全
一.C#中字符串的建立过程 例如定义变量 strT="Welcome to "; strT+="www.cuit.edu.cn"; 程序首先创建一个System ...
- javascript中字符串常用操作总结、JS字符串操作大全
字符串的操作在js中非常频繁,也非常重要.以往看完书之后都能记得非常清楚,但稍微隔一段时间不用,便会忘得差不多,记性不好是硬伤啊...今天就对字符串的一些常用操作做个整理,一者加深印象,二者方便今后温 ...
- JS字符串操作大全
String对象属性 (1) length属性 length算是字符串中非常常用的一个属性了,它的功能是获取字符串的长度.当然需要注意的是js中的中文每个汉字也只代表一个字符,这里可能跟其他语言有些不 ...
- js--javascript中字符串常用操作总结、JS字符串操作大全
字符串的操作在js中非常频繁,也非常重要.以往看完书之后都能记得非常清楚,但稍微隔一段时间不用,便会忘得差不多,记性不好是硬伤啊...今天就对字符串的一些常用操作做个整理,一者加深印象,二者方便今后温 ...
- shell字符串操作之cut---实现字符串截取
shell中(字符串截取) cut是以每一行为一个处理对象的,这种机制和sed是一样的.(关于sed的入门文章将在近期发布) 2 cut一般以什么为依据呢? 也就是说,我怎么告诉cut我想定位到的剪切 ...
- linux的string操作(字符串截取,长度计算)
按指定的字符串截取 1.第一种方法: ${varible##*string} 从左向右截取最后一个string后的字符串 ${varible#*string}从左向右截取第一个string后的字符串 ...
- C#字符串操作大全
===============================字符串基本操作================================ 一.C#中字符串的建立过程 例如定义变量 strT=&qu ...
- web 前端 常见操作 将时间戳转成日期格式 字符串截取 使用mui制作选项卡
1.将时间戳转成日期格式: //第一种 function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString( ...
- js-DOM ~ 05. Date日期的相关操作、string、查字符串的位置、给索引查字符、字符串截取slice/substr/substring、去除空格、替换、大小写、Math函数、事件绑定、this
内置对象: 语言自带的对象/提供了常用的.基本的功能 打印数组和字符串不用for... in / 打印josn的时候采用for...in Date 获取当前事件: var date = ...
随机推荐
- INV*更新物料信息
物料 PROCEDURE update_item(p_init_msg_list IN VARCHAR2 DEFAULT fnd_api.g_false, x_return_status OUT NO ...
- 【php写日志】php将日志写入文件
php 写内容到文件,把日志写到log文件 <?php header("Content-type: text/html; charset=utf-8"); /******** ...
- android中文字中间有超链接的实现方法
1.XML里写: <resources> <string name="ACCOUNT_REGISTER_PROMPT_AGREEMENT">点击注册,表 ...
- 转 Android开发学习笔记:浅谈WebView
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://liangruijun.blog.51cto.com/3061169/647456 ...
- 使用 C# 开发智能手机软件:推箱子(十八)
这是"使用 C# 开发智能手机软件:推箱子" 系列文章的第十八篇.在这篇文章中.介绍 Window/SelectLevelDlg.cs 源程序文件. 这个源程序文件包括 Selec ...
- Struts2源码阅读(一)_Struts2框架流程概述
1. Struts2架构图 当外部的httpservletrequest到来时 ,初始到了servlet容器(所以虽然Servlet和Action是解耦合的,但是Action依旧能够通过httpse ...
- lo4j 日志级别
日志记录器(Logger)的行为是分等级的.如下表所示: 分为OFF.FATAL.ERROR.WARN.INFO.DEBUG.TRACE.ALL或者您定义的级别.Log4j建议只使用四个级别,优先级从 ...
- 删除outlook配置信息
1.输入“Win+R”组合键,在弹出的窗口中输入:control,打开控制面板 2.找到“邮件”选项,并单击 3.在弹出的窗口中,单击“显示配置文件”选项,删除配置文件夹,OK.
- Eclipse “cannot be resolved to a type” error
引言: eclipse新导入的项目经常可以看到"XX cannot be resolved to a type"的报错信息.本文将做以简单总结. 正文: (1)jd ...
- 【pywin32总结】
#下面是必备的#注意!所有方法后面都要加括号()!!! import win32com from win32com.client import Dispatch,constants w = win32 ...