<?php
header("Content-Type:text/html;charset=utf-8");
function msubstr($str, $start = 0, $length, $lenth2, $suffix = true){
//$length 中文截取长度,$lenth2英文截取长度 $suffix 是否省略号
$charset = 'utf-8';
if (LANG_SET != 'zh'){
$length = $lenth2;
}
$str = preg_replace("/(\<[^\<]*\>|\r|\n|\s|\[.+?\])/is", ' ', $str);
if (function_exists("mb_substr"))
$slice = mb_substr($str, $start, $length, $charset);
elseif (function_exists('iconv_substr')) {
$slice = iconv_substr($str, $start, $length, $charset);
if (false === $slice) {
$slice = '';
}
} else {
$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));
}
$fix = '';
if (LANG_SET == 'zh') {
$slice = str_replace(' ', '', $slice);
if (strlen($slice) > $length) {
$fix = '...';
}
} else {
if (strlen($str) > $lenth2) {
$fix = '...';
}
}
return $suffix ? $slice . $fix : $slice;
} $str = "dsd个法师dsad打发dd的撒ddsddsadsadsads的撒"; echo msubstr($str,1,2,20,true); ?>

php多语言截取字符串函数的更多相关文章

  1. ThinkPHP 模板substr的截取字符串函数

    ThinkPHP 模板substr的截取字符串函数在Common/function.php加上以下代码 /** ** 截取中文字符串 **/ function msubstr($str, $start ...

  2. Thinkphp内置截取字符串函数

    Thinkphp内置了一个可以媲美smarty的模板引擎,给我们带来了很大的方便.调用函数也一样,可以和smarty一样调用自己需要的函数,而官方也内置了一些常用的函数供大家调用. 比如今天我们说的截 ...

  3. PHP截取字符串函数substr()函数实例用法详解

    在PHP中有一项非常重要的技术,就是截取指定字符串中指定长度的字符.PHP对于字符串截取可以使用PHP预定义函数substr()函数来实现.下面就来介绍一下substr()函数的语法及其应用. sub ...

  4. mysql 截取字符串 函数

    文章摘取自http://www.cnblogs.com/zdz8207/p/3765073.html 练习截取字符串函数(五个) mysql索引从1开始 一.mysql截取字符串函数 1.left(s ...

  5. SQL注入截取字符串函数

    在sql注入中,往往会用到截取字符串的问题,例如不回显的情况下进行的注入,也成为盲注,这种情况下往往需要一个一个字符的去猜解,过程中需要用到截取字符串.本文中主要列举三个函数和该函数注入过程中的一些用 ...

  6. SQL截取字符串函数

    A.截取从字符串左边开始N个字符 以下是代码片段:    Declare @S1 varchar(100)  Select @S1='http://www.xrss.cn'  Select Left( ...

  7. java截取字符串函数

    substring public String substring(int beginIndex)返回一个新的字符串,它是此字符串的一个子字符串.该子字符串始于指定索引处的字符,一直到此字符串末尾. ...

  8. Oracle截取字符串函数和查找字符串函数,连接运算符||

    参考资料:Oracle截取字符串和查找字符串 oracle自定义函数学习和连接运算符(||) oracle 截取字符(substr),检索字符位置(instr) case when then else ...

  9. MySQL截取字符串函数方法

    函数: 1.从左开始截取字符串 left(str, length) 说明:left(被截取字段,截取长度) 例:select left(content,200) as abstract from my ...

随机推荐

  1. POI导出

    public void export(){try {String columns = getPara("nameArray");List<Record> list = ...

  2. 把UIView转成UIImage,解决模糊失真问题

    近期工作中,遇到一个需求,须要把一个UIView对象转成UIImage对象显示.经过网络搜索.找到例如以下答案: ? 1 2 3 4 5 6 7 8 -(UIImage*)convertViewToI ...

  3. PHP5.5四种序列化性能对照

    json_encode,serialize,igbinary,msgpack四种序列化方式,在之前已经有过相关的測试,PHP5.5这方面的測试临时没有,这次測试基于PHP5.5,而且測试用例,http ...

  4. CCNotificationCenter(一)

    const std::string testsName[MAX_COUNT] = { "Bug-350", "Bug-422", "Bug-458&q ...

  5. Python import random报错处理办法

    [转自]http://blog.chinaunix.net/uid-26000296-id-4356738.html python安装失败:make的时候报错: /usr/include/tkDecl ...

  6. js需要学习的

    1.js作用域 2.html5和css3 3.js面向对象 4.js原生ajax 5.js与后端交互 6.html5手机移动端页面练习 7.jquery中的jQuery UI框架和jQuery Eas ...

  7. js字母大小写字母转换

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...

  8. Filter详解

    转自 http://blog.csdn.net/yudaoai/article/details/4231333 filter功能.它使用户可以改变一个 request和修改一个response. Fi ...

  9. Python的内存管理 小理解

    请看下面的一段代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 origin = {'a':100,'b':[1,2,34,5]} obj_copy ={}; ...

  10. FreeRTOS 低功耗之停机模式

    以下转载自安富莱电子: http://forum.armfly.com/forum.php STM32F103 如何进入停机模式在 FreeRTOS 系统中,让 STM32 进入停机模式比较容易,调用 ...