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 工厂模式使用方法

基本的工厂类 复制代码 代码如下: class MyObject{ //对象将从工厂返回 } class MyFactory{ public static function factor...

PHP4和PHP5共存于一系统

PHP4和PHP5共存于一系统˂!-- google 的广告条 2005年09月20日换位置 唉,22号被停了.郁闷,没作弊呀 11.27日重开了 ˂!-- googl...

php中explode函数用法分析

本文实例分析了php中explode函数用法。分享给大家供大家参考。具体如下: explode(string separator,string string [,int limit])...

解决file_get_contents无法请求https连接的方法

错误: Warning: fopen() [function.fopen]: Unable to find the wrapper "https" - did you forget to...

php反射学习之不用new方法实例化类操作示例

本文实例讲述了php反射学习之不用new方法实例化类操作。分享给大家供大家参考,具体如下: 上一篇php反射入门示例简单介绍了 php 反射的几个常见类的使用方法,但是用反射能做些什么,...