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调用Webservice实例代码

它是一个开源软件,是完全采用PHP语言编写的、通过HTTP收发SOAP消息的一系列PHP类,由NuSphere Corporation(http://dietrich.ganx4.com...

探讨如何在PHP开启gzip页面压缩实例

示例一(用php的内置压缩函数):复制代码 代码如下:<?PHP if(Extension_Loaded('zlib')) Ob_Start('ob_gzhandler'); He...

php实现往pdf中加数字签名操作示例【附源码下载】

本文实例讲述了php实现往pdf中加数字签名操作。分享给大家供大家参考,具体如下: //===============================================...

php读取出一个文件夹及其子文件夹下所有文件的方法示例

本文实例讲述了php读取出一个文件夹及其子文件夹下所有文件的方法。分享给大家供大家参考,具体如下: 今天的需求要在一个文件夹中读取出这个文件夹下所有的文件,当然也包括这个文件夹下面所有的...

Cygwin中安装PHP方法步骤

1.在Cygwin中安装apt-cyg包管理工具 复制代码 代码如下: $ wget http://apt-cyg.googlecode.com/svn/trunk/apt-cyg $...