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, ' ');

相关文章

php下用GD生成生成缩略图的两个选择和区别

php下用GD生成生成缩略图的两个选择和区别

PHP的GD扩展提供了两个函数来缩放图像:ImageCopyResized(dest, src, dx, dy, sx, sy, dw, dh, sw, sh);ImageCopyRes...

PHP检测接口Traversable用法详解

本文实例讲述了PHP检测接口Traversable用法。分享给大家供大家参考,具体如下: Traversable用于检测一个类是否可以使用 foreach 进行遍历,这是一个无法在 PH...

php实现将二维关联数组转换成字符串的方法详解

本文实例讲述了php实现将二维关联数组转换成字符串的方法。分享给大家供大家参考,具体如下: 需求 项目中遇到了二维关联数组转字符串的问题,查阅相关资料,写了如下程序,并且能过滤重复的关键...

php根据年月获取当月天数及日期数组的方法

本文实例讲述了php根据年月获取当月天数及日期数组的方法。分享给大家供大家参考,具体如下: function get_day( $date ) { $tem = expl...

php5.6.x到php7.0.x特性小结

本文总结分析了php5.6.x到php7.0.x特性。分享给大家供大家参考,具体如下: php5.6.x到php7.0.x特性 1.标量类型声明 字符串(string), 整数 (int...