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对mongodb的扩展(初出茅庐)

我们的php mongodb也能做mysql、sqlserver能做的几乎所有功能,本文将详细介绍 一、操作符 操作符相信大家肯定都知道了,就是等于、大于、小于、不等于、大于等于、小于等...

在WordPress的后台中添加顶级菜单和子菜单的函数详解

在WordPress的后台中添加顶级菜单和子菜单的函数详解

添加设置页面-add_menu_page函数 add_menu_page(),这个函数是往后台添加顶级菜单先,也就是和“外观”、“插件”等一样的顶级菜单。 函数介绍如下: <&...

php防注

$_POST = sql_injection($_POST); $_GET = sql_injection($_GET); function&nb...

php UTF8 文件的签名问题

也就是 有BOM 格式编码,或者 无BOM格式编码。 如果看文件的内容,是看不出任何差别的,以下列文件(schema.sqlite.sql)内容为例: schema.sqlite.sql...

phpword插件导出word文件时中文乱码问题处理方案

最近一个项目开发要用到PHP技术导出Word文档,比较了几种方案,首先是使用Microsoft Office自带的ActiveX/COM组件,比如Word.Application,这种方...