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 ...
随机推荐
- Android:你不知道的 WebView 使用漏洞
前言 如今非常多App里都内置了Web网页(Hyprid App),比方说非常多电商平台.淘宝.京东.聚划算等等.例如以下图 上述功能是由 Android的WebView 实现的.可是 WebView ...
- Django实现任意文件上传(最简单的方法)
利用Django实现文件上传并且保存到指定路径下,其实并不困难,完全不需要用到django的forms,也不需要django的models,就可以实现,下面开始实现. 第一步:在模板文件中,创建一个f ...
- 异常处理:1215 - Cannot add foreign key constraint
最近在做新生入学系统,学生表中包括新生的班级,专业等信息,班级,专业就需要和班级表,专业表进行关联,但是在添加外键的过程中却出现了“Cannot add foreign key constraint” ...
- 【NOI】9272 偶数个三
题目 链接:bajdcc/ACM 描述 在所有的N位数中,有多少个数中有偶数个数字3?结果模12345.(1<=N<=10000) 样例输入 2 样例输出 73 方法一:穷举 评价:最简单 ...
- 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 ...
- Mac 命令行安装mysql homebrew 安装mysql后,如何配置mysql
非常好 强力推荐 这个是我最新并且一直推崇的方法:1.安装:sunyichaodeMacBook-Pro:~ sunyichao$ brew install mysql2.开启mysql:mysql. ...
- C++使用ocilib访问oracle数据库
引用: http://blog.csdn.net/u011311985/article/details/51221898 测试程序我放到 http://download.csdn.NET/detail ...
- jfinal渲染器FileRender完整路径文件不正确的问题
jfinal作者的建议如下: 完整分支的文件下载,可以使用那个带 File 参数的构造方法:FileRender(new File(完整路径)) 从而可以使用 renderFile(new File( ...
- nio入门教程
1.通过拿NIO和传统IO做对比来了解NIO 面向流与面向缓冲 Java NIO和IO之间第一个最大的区别是,IO是面向流的,NIO是面向缓冲区的. Java IO面向流意味着每次从流中读一个或多个字 ...
- web编码
1各种编码 A .1 html编码 -HTML标签 this.Response.Write(this.Server.HtmlEncode("<h1>的作用将文本设置为标题样式! ...