PHP在字符断点处截断文字的实现代码

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

//所谓断字 (word break),即一个单词可在转行时断开的地方。这一函数将在断字处截断字符串。
// Please acknowledge use of this code by including this header.
function myTruncate($string, $limit, $break=".", $pad="...") {
// return with no change if string is shorter than $limit
if(strlen($string) <= $limit)
return $string;
// is $break present between $limit and the end of the string?
if(false !== ($breakpoint = strpos($string, $break, $limit))) {
if($breakpoint < strlen($string) - 1) {
$string = substr($string, 0, $breakpoint) . $pad;
}
}
return $string;
}
/***** Example ****/
$short_string=myTruncate($long_string, 100, ' ');

相关文章

PHPUnit PHP测试框架安装方法

单元测试是几个现代敏捷开发方法的基础,使得PHPUnit成为许多大型PHP项目的关键工具。这个工具也可以被Xdebug扩展用来生成代码覆盖率报告 ,并且可以与phing集成来自动测试,最...

域名和cookie问题(域名后缀)

域名和cookie问题(域名后缀)

域名和cookie 偶然想到一个问题:www.g.cn能把cookie设置为.g.cn,那么www.com.cn能设置把cookie设置为.com.cn吗? 试验结果:不能。因为浏览器知...

THINKPHP2.0到3.0有哪些改进之处

THINKPHP2.0到3.0有哪些改进之处

1.thinkphp中我们的入口文件写法,可能最后要加一个app::run(); 3.0就完全不用了,你会发现不然会出现两次调用2.debug 我们开发的时候在2.0中我们一般都这样写...

PHP爆绝对路径方法收集整理

1、单引号爆路径 说明: 直接在URL后面加单引号,要求单引号没有被过滤(gpc=off)且服务器默认返回错误信息。 Eg: www.xxx.com/news.php?id=149′ 2...

深入理解PHP之require/include顺序 推荐

也就有了如下的疑问: include_path是怎么起作用的? 如果有多个include_path顺序是怎么样的? 什么情况下include_path不起作用? 今天, 我就全面的介绍下...