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实现Session存储到Redis

对于大访问量的站点使用默认的Session 并不合适,我们可以将其存入数据库、或者使用Redis KEY-VALUE数据存储方案 首先新建一个session表 CREATE TAB...

php实现购物车功能(下)

php实现购物车功能(下)

接着上篇继续学习: 《php实现购物车的功能(上)》 7、实现一个管理界面 登录界面 由以下代码实现: 7.1 admin.php <?php /**...

PHP性能测试工具xhprof安装与使用方法详解

本文实例分析了PHP性能测试工具xhprof安装与使用方法。分享给大家供大家参考,具体如下: xhprof概述: XHProf是一个分层PHP性能分析工具。它报告函数级别的请求次数和各种...

php图片加中文水印实现代码分享

例1 复制代码 代码如下: <?php Header("Content-type: image/png"); /*通知浏览器,要输出图像*/ $im = imagecreate(4...

PHP压缩图片功能的介绍

php程序开发中经常涉及到生成缩略图,利用php生成缩略图这个过程本身没难度,但是你知道php能够优化调节生成的缩略图的质量吗?也就是说php能够控制生成缩略图的清晰度以及生成后的缩略图...