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

相关文章

fleaphp crud操作之find函数的使用方法

find函数的原型 复制代码 代码如下: /** * 返回符合条件的第一条记录及所有关联的数据,查询没有结果返回 false * * @param mixed $conditions *...

php实现的数组转xml案例分析

本文实例讲述了php实现的数组转xml。分享给大家供大家参考,具体如下: 0x00 需求 最近要做百度、360、神马搜索的网站sitemap,三家的格式都是xml,然而具体的细节还有有差...

PHP中两个float(浮点数)比较实例分析

本文实例讲述了PHP中两个float(浮点数)比较方法。分享给大家供大家参考。具体如下: 最近在开发一个合同管理系统的时候,涉及到两个浮点数比较,算是把我郁闷惨了。 在N久以前,就不晓得...

Fatal error: session_start(): Failed to initialize storage module: files问题解决方法

之前编译安装的LNMP环境+phpmyamdin4.02的版本,今天突然出现这个问题:复制代码 代码如下:Fatal error: session_start(): Failed to...

php获取数组中键值最大数组项的索引值 原创

本文实例讲述了php获取数组中键值最大数组项的索引值的方法。分享给大家供大家参考。具体分析如下: 一、问题: 从给定数组中获取值最大的数组项的键值。用途如:获取班级得分最高的学生的姓名。...