php 中文切割字符串长度
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
if(function_exists("mb_substr")) {
if($suffix)
{
if($str==mb_substr($str, $start, $length, $charset))
{
return mb_substr($str, $start, $length, $charset);
}
else
{
return mb_substr($str, $start, $length, $charset)."...";
}
}
else
{
return mb_substr($str, $start, $length, $charset);
}
}
elseif(function_exists('iconv_substr')) {
if($suffix)
{
if($str==iconv_substr($str,$start,$length,$charset))
{
return iconv_substr($str,$start,$length,$charset);
}
else
{
return iconv_substr($str,$start,$length,$charset)."...";
}
}
else
{
return iconv_substr($str,$start,$length,$charset);
}
}
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("",array_slice($match[0], $start, $length));
if($suffix) return $slice."…";
return $slice;
}
php 中文切割字符串长度的更多相关文章
- php使用strlen()判断中文汉字字符串长度
php使用strlen()判断中文汉字字符串长度 对于含有中文情况,此时可以采用: iconv_strlen($str,"UTF-8"); iconv_strlen 是无论是何种编 ...
- js获取(包括中文)字符串长度与截取字符串
/** * @param begin 截取开始的索引 * @param num 截取的长度 */ //截取字符串(包括中文) function SetString(str, len) { var st ...
- Javascript校验含中文的字符串长度
原文地址:http://sozai.wlling.com/html/Tutorial/JavaScript/2009/0214/2936.html /** * 得到字符串的字符长度(一个汉字占两个字符 ...
- java处理含有中文的字符串.
1. 问题描述: 原始数据是以行为单位的, 每行固定长度931个字节, 汉字占2个字节, 按照字典描述,共有96个字典,只有第32个字典为中文地址, 所以需要单独处理. 由于项目设计保密,故删除敏感数 ...
- JS判断字符串长度(中文长度为2,英文长度为1)
目的:计算字符串长度(英文占1个字符,中文汉字占2个字符) 方法一: String.prototype.gblen = function() { var len = 0; for (var i=0; ...
- (实用篇)php中计算中文字符串长度、截取中文字符串的函数代码
在PHP中,我们都知道有专门的mb_substr和mb_strlen函数,可以对中文进行截取和计算长度,但是,由于这些函数并非PHP的核心函数,所以,它们常常有可能没有开启.当然,如果是用的自己的服务 ...
- php计算字符串长度:utf8编码,包含中文
php计算字符串长度:utf8编码 中文当作1个字符处理(strlen默认当作两个字符) 上函数: /** * 计算 UTF-8 字符串长度 * * @param string $str * @ret ...
- JS~字符串长度判断,超出进行自动截取(支持中文)
今天一个小弟问我的问题,在文本框中输入字符,如果超出指定长度,就把它截取,要求中文等于两个字符的长度,我找一下资料,把这个功能实现了,下面是JS代码: <html> <script ...
- php中计算中文字符串长度、截取中文字符串
在做PHP开发的时候,由于我国的语言环境问题,所以我们常常需要对中文进行处理.在PHP中,我们都知道有专门的mb_substr和mb_strlen函数,可以对中文进行截取和计算长度,但是,由于这些函数 ...
随机推荐
- codeforces--376D--376F(前缀和优化)
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- php 创建删除数据库
<?php $dbhost = 'localhost:3306'; $dbuser = 'root'; $dbpass = ''; $conn = mysql_connect($dbhost, ...
- java 基础之概念
1:栈(Stack) 先进后出(邮政模拟例) 2:队列(Queue) 先进先出(排队购票)
- kvm的live-snapshot
目前项目中已经存在的快照是针对卷的快照,并且需要关机.所以目前的需求有两个:1.不关机快照:2.针对虚拟机的快照,而不是针对券的快照. 由需求所以针对libvirt做了一些实验,纪录如下: 环境:物理 ...
- PAT (Advanced Level) 1102. Invert a Binary Tree (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- Oracle 使用sql创建表空间及用户
create tablespace OrcalDBNamedb datafile 'C:\OracleDBDirc\OrcalDBNamedb.dbf' size 300m; 创建用户create u ...
- jquery插件简单的声明方法
(function($){ // 声明插件内容 $.fn.pluginName = function(options){ // 默认插件配置 var ...
- Eclipse配置
下载地址:http://www.eclipse.org/downloads/ tomcat plugin:http://www.eclipsetotale.com/tomcatPlugin.html# ...
- source.list
deb http://www.anheng.com.cn/debian/ squeeze maindeb-src http://www.anheng.com.cn/debian/ squeeze ma ...
- 【转】The magic behind array length property
Developer deals with arrays every day. Being a collection, an important property to query is the num ...