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 函数使用可变数量的参数方法

php在用户自定义函数中支持可变数量的参数列表。 在php5.5及更早的版本中,使用func_num_args(), func_get_arg(), func_get_args()函数实...

如何从一个php文件向另一个地址post数据,不用表单和隐藏的变量的

可以使用以下函数来实现:  <?php function posttohost($url, $data) { $url =&nbs...

Windows下XDebug 手工配置与使用说明

1. 下载XDebug二进制文件: http://www.xdebug.org/download.php   5.2 http://www.xdebug.org/files/php_xd...

$_GET['goods_id']+0 的使用详解

目的: 为了防止sql注入,tid,goods_id都是正整数类型,防止人为了在后面追加 ?tid=1 or 1 这样的语句.原理: 不管你的参数多么险恶,+0后都老老实实变成数值类型...

PHP递归遍历指定目录的文件并统计文件数量的方法

本文实例讲述了PHP递归遍历指定目录的文件并统计文件数量的方法。分享给大家供大家参考。具体实现方法如下: <?php //递归函数实现遍历指定文件下的目录与文件数量 f...