php多语言截取字符串函数
<?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多语言截取字符串函数的更多相关文章
- ThinkPHP 模板substr的截取字符串函数
ThinkPHP 模板substr的截取字符串函数在Common/function.php加上以下代码 /** ** 截取中文字符串 **/ function msubstr($str, $start ...
- Thinkphp内置截取字符串函数
Thinkphp内置了一个可以媲美smarty的模板引擎,给我们带来了很大的方便.调用函数也一样,可以和smarty一样调用自己需要的函数,而官方也内置了一些常用的函数供大家调用. 比如今天我们说的截 ...
- PHP截取字符串函数substr()函数实例用法详解
在PHP中有一项非常重要的技术,就是截取指定字符串中指定长度的字符.PHP对于字符串截取可以使用PHP预定义函数substr()函数来实现.下面就来介绍一下substr()函数的语法及其应用. sub ...
- mysql 截取字符串 函数
文章摘取自http://www.cnblogs.com/zdz8207/p/3765073.html 练习截取字符串函数(五个) mysql索引从1开始 一.mysql截取字符串函数 1.left(s ...
- SQL注入截取字符串函数
在sql注入中,往往会用到截取字符串的问题,例如不回显的情况下进行的注入,也成为盲注,这种情况下往往需要一个一个字符的去猜解,过程中需要用到截取字符串.本文中主要列举三个函数和该函数注入过程中的一些用 ...
- SQL截取字符串函数
A.截取从字符串左边开始N个字符 以下是代码片段: Declare @S1 varchar(100) Select @S1='http://www.xrss.cn' Select Left( ...
- java截取字符串函数
substring public String substring(int beginIndex)返回一个新的字符串,它是此字符串的一个子字符串.该子字符串始于指定索引处的字符,一直到此字符串末尾. ...
- Oracle截取字符串函数和查找字符串函数,连接运算符||
参考资料:Oracle截取字符串和查找字符串 oracle自定义函数学习和连接运算符(||) oracle 截取字符(substr),检索字符位置(instr) case when then else ...
- MySQL截取字符串函数方法
函数: 1.从左开始截取字符串 left(str, length) 说明:left(被截取字段,截取长度) 例:select left(content,200) as abstract from my ...
随机推荐
- 关于VS中的调试信息输出
有时候一些项目的调试信息不方便输出到界面中,比如ASP.NET或者WPF之类的 可以使用Debug.WriteLine()等方法输出到"输出"窗口,不过"输出" ...
- 黑客编程教程(一)了解Windows机制
第一节 了解Windows机制 Windows 是一个“基于事件的,消息驱动的”操作系统. 在Windows下执行一个程序,只要用户进行了影响窗口的动作(如改变窗口大小或移动.单击鼠标等)该动作就会触 ...
- Oracle数据库操作类及连接方法
创建DataBaseAccess引用库下面包括DBConnectionString.cs,DBType.cs ,SysName.cs 三个类 DBConnectionString: using Sys ...
- Along with all the above benefits, you cannot overlook the space efficiency and performance gains in using DataFrames and Dataset APIs for two reasons.
Of all the developers’ delight, a set of APIs that makes them productive, that are easy to use, and ...
- 手动挡C1驾驶学车@长建驾校
2015-11-01 下午取钱去长建驾校报名学车 4600(学费) + 100(暂住证) + 30(照片) + 10(体检) 2015-11-14 8:00-12:00 理论课2 光盘10 2015 ...
- LeetCode: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- Redis Keys 命令 - 查找所有符合给定模式( pattern)的 key
Redis Keys 命令用于查找所有符合给定模式 pattern 的 key .. 语法 redis KEYS 命令基本语法如下: redis 127.0.0.1:6379> KEYS PAT ...
- 多核CPU上python多线程并行的一个假象
GIL 与 Python 线程的纠葛 GIL 是什么东西?它对我们的 python 程序会产生什么样的影响?我们先来看一个问题.运行下面这段 python 程序,CPU 占用率是多少? # 请勿在工作 ...
- [Makefile]多文件的通用Makefile
下面是一个糅合多线程和多文件的示例 emc-test.c #include <stdio.h> #include <pthread.h> #include "char ...
- linux内存查看及释放
查看内存 常用的查看内存工具有:top,ps,free,/proc/meminfo,/proc/$PID/status等,一般都指定了虚拟内存占用情况,但ps或/proc/$PID/status中RS ...