PHP在字符断点处截断文字的实现代码
作者:bea
代码如下: //所谓断字 (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 th
代码如下:
//所谓断字 (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, ' ');
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- FirePHP 推荐一款PHP调试工具
- 在smarty模板中使用PHP函数的方法
- 对text数据类型不支持代码页转换 从: 1252 到: 936
- fleaphp crud操作之find函数的使用方法
- fleaphp crud操作之findByField函数的使用方法
- fleaphp常用方法分页之Pager使用方法
- PHP中限制IP段访问、禁止IP提交表单的代码
- PHP计划任务、定时执行任务的实现代码
- PHP导入Excel到MySQL的方法
- 在php和MySql中计算时间差的方法
- PHP遍历二维数组的代码
- PHP中调用ASP.NET的WebService的代码
- php中利用post传递字符串重定向的实现代码
- php将fileterms函数返回的结果变成可读的形式
- php压缩多个CSS为一个css的代码并缓存
- flash用php连接数据库的代码
- php中用数组的方法设置cookies
- php 获取全局变量的代码
- php强制下载类型的实现代码