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类自动装载、链式操作、魔术方法实现代码

1、自动装载实例 目录下有3个文件:index.php load.php tests文件夹 tests文件夹里有 test1.php <?php namespace T...

对text数据类型不支持代码页转换 从: 1252 到: 936

对 text 数据类型不支持代码页转换。从: 1252 到: 936。 ———————————————————— 该问题是由于设计标的时候,原数据库采用的是: 英语(美国) 0×409...

php error_log 函数的使用

我们来大致了解一下error_log()函数。我们看下手册的解释: error_log(PHP 3, PHP 4, PHP 5) bool error_log ( string mess...

hessian 在PHP中的使用介绍

hessian 在PHP中的使用介绍

一、hessian是什么? 看到这个单词我还不知道怎么读,音标是[hes]读黑森。 Hessian是一个轻量级的远程的数据交换工具,使用简单的方法提供了RMI(远程方法调用)的功能. 相...

Yii操作数据库的3种方法

一、执行原生太SQL的PDO方式。复制代码 代码如下:$sql = "";//原生态sql语句 xx::model()->dbConnection->createComman...