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系统命令函数使用分析

复制代码 代码如下:function execute($cmd) {     $res = '';    ...

支持png透明图片的php生成缩略图类分享

支持png透明图片的php生成缩略图类分享

注:此功能依赖GD2图形库 最近要用php生成缩略图,在网上找了一下,发现了这篇文章:PHP生成图片缩略图 试用了一下后,发现有这样几个问题: 1、png图片生成的缩略图是jpg格式的...

php简单判断文本编码的方法

本文实例讲述了php简单判断文本编码的方法。分享给大家供大家参考。具体如下: 这里通过对文本的一次循环编码,来判断是否属于该编码。 public function chkCode($...

php实现的发送带附件邮件类实例

本文实例讲述了php实现的发送带附件邮件类的方法,是一个非常实用的功能。分享给大家供大家参考。具体方法如下: emailclass.php类文件如下: <? clas...

使用PHP编写的SVN类

复制代码 代码如下:<?php/** * SVN 外部命令 类 * * @author rubekid * * @todo com...