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解压ZIP文件到指定文件夹的方法

本文实例讲述了PHP解压ZIP文件到指定文件夹的方法。分享给大家供大家参考,具体如下: /** * function: 解压zip 格式的文件 * author:friker...

php读取目录所有文件信息dir示例

复制代码 代码如下: <?php set_time_limit(0); function tree($directory) { $mydir=dir($directory); ec...

PHPCMS的使用小结

下面我讲讲关于这套系统的加载流程 定义根目录,定义include目录 加载核心文件 配置文件'config.inc.php' 全局函数'global.func.php' 目录操作函数'd...

php7新特性的理解和比较总结

php7新特性的理解和比较总结

1. null合并运算符(??) 语法: 如果变量存在且值不为NULL,它就会返回自身的值,否则返回它的第二个操作数. //php7以前 if判断 if(emp...

PHP 进度条函数的简单实例

PHP 进度条函数的简单实例

PHP 进度条函数的简单实例 其实进度条的做法很简单的。网上的一大堆,自己写了一个,哈哈,感觉看起来很有感觉。 实例代码: function ShowPercent($now,$to...