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 ...
随机推荐
- Java并发和多线程那些事儿
我记得我接触电脑的时候是在小学三年级的时候,那是1995年,那年发布了windows95,但是我学习的时候还是只是dos系统,简单对于文件的一些命令操作还有五笔 在过去的那个年代,电脑都是单CPU,也 ...
- [na]ppp协议链路认证-chap认证流程
Point-to-Point Protocol (PPP)协议是广域网链路的一种协议,不同于局域网的ethernetII协议 PPP协商过程,分三步:LCP.认证.NCP. 一 协议概述 PPP包含以 ...
- [leetcode] Longest Palindromic Substring 多种解法
非常经典的题目,求字符串中的最长回文子串. (1)最朴素的解法 ---暴力 复杂度O(N³) 这也是最easy想到的方法.最外层循环枚举起点i,第二层循环从i+1開始向后枚举,第三层推断是不是回文串. ...
- Linux音频编程指南(转)
转自: http://www.ibm.com/developerworks/cn/linux/l-audio/ Linux音频编程指南 虽然目前Linux的优势主要体现在网络服务方面,但事实上同样也有 ...
- Java – Convert IP address to Decimal Number
In this tutorial, we show you how to convert an IP address to its decimal equivalent in Java, and vi ...
- Android开发日记(三)
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV ...
- java华为面试题
JAVA方面 1 面向对象的特征有哪些方面 2 String是最基本的数据类型吗? 3 int 和 Integer 有什么区别 4 String 和StringBuffer的区别 5运行时异常与一般异 ...
- C++笔记 3
1.数组是自动分配空间,指针要手工分配空间(int *p = new int;) 2.在Unix上,程序出现段错误的时候,系统会生成core 文件,会把出现错误的那一刻的程序镜像保存在此文件中 3.结 ...
- linux下getrlimit与sysconf函数
#include <stdio.h> #include <sys/time.h> #include <sys/resource.h> int main(int ar ...
- 风雪之隅(Laruence PHP开发组成员, Zend兼职顾问, Yaf, Yar, Yac, Opcache等项目作者、维护者.)
http://www.laruence.com/?from=inf&wvr=5&loc=infblog