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

相关文章

在html文件中也可以执行php语句的方法

在apache的conf目录中修改文件httpd.conf。 搜索AddType application/ 找到前面没有#的那条,在下面添加AddType application/x-h...

PHP学习笔记 (1) 环境配置与代码调试

一配置PHP环境 1、了解什么是PHP        PHP(“PHP: Hypertext Preprocessor”,超...

php实现购物车功能(上)

php实现购物车功能(上)

本文分两篇为大家介绍php实现购物车功能,具有一定的参考价值,相信大家一定喜欢。 1、需求分析  我们需要找到一种将数据库连接到用户的浏览器的方法。用户能够按目录浏览商品。&n...

php 指定范围内多个随机数代码实例

调用mt_rand()这个方法可以生成随机数字,参数是范围的最小值和最大值,函数会返回最小值和最大值之间的一个随机数字。 要生成真正的随机数,对于计算来说不是一件容易的事。 php中两...

PHP统一页面编码避免乱码问题

页面编码统一 MySQL数据库编码、html页面编码、PHP或html文件本身编码要全部一致。 1、MySQL数据库编码: 建立数据库时指定编码(如gbk_chinese_ci),建立...