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实现的二分查找算法示例

本文实例讲述了php实现的二分查找算法。分享给大家供大家参考,具体如下: <?php $arr = array(4,58,11,34,88,45,32,54,63,78...

PHP函数func_num_args用法实例分析

本文实例讲述了PHP函数func_num_args用法。分享给大家供大家参考,具体如下: function foo() { $numargs = func_num_args()...

使用phpQuery采集网页的方法

phpQuery是一个基于PHP的服务端开源项目,它可以让PHP开发人员轻松处理DOM文档内容,比如获取某新闻网站的头条信息。更有意思的是,它采用了jQuery的思想,你可以像使用jQu...

php中记录用户访问过的产品,在cookie记录产品id,id取得产品信息

1.测试方法www.xxx.com/test.php?content_id=自己定义,如:44 复制代码 代码如下: $content_id = array();//1.创建一个数组 $...

phpinfo()中Loaded Configuration File(none)的解决方法

phpinfo()中Loaded Configuration File(none)的解决方法

前言 单独编译php7,并安装在/usr/local/php7/中,今天开发插件修改了 php.ini 的配置信息,但是什么都没生效。 排查 通过phpinfo()查看配置信息:...