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, ' ');

相关文章

Mac系统下使用brew搭建PHP(LNMP/LAMP)开发环境

Mac下搭建lamp开发环境很容易,有xampp和mamp现成的集成环境。但是集成环境对于经常需要自定义一些配置的开发者来说会非常麻烦,而且Mac本身自带apache和php,在brew...

PHP中explode函数和split函数的区别小结

一、前言 之所以做这个,是因为这两个函数的作用很像,都是把字符串转换成数组。 二、explode 从下面的例子可以看出,生成的数组是有对应的顺序的。 $pizza = "piece1...

php操作sqlserver关于时间日期读取的小小见解

上周五,要做一个php 同时对mysql数据库和sqlserver数据库的连接东东 以前一直在用mysql对sqlserver不是很熟悉,于是摸着石头过河。没有别的至少mysql和sql...

php is_file()和is_dir()用于遍历目录时用法注意事项

1、目录inc有以下内容: 子目录 0 子目录 a footer.html header.html login_function.inc.php mysqli_connect.php s...

[PHP]实用函数2

1、产生随机字符串函数 复制代码 代码如下:function random($length) {  $hash = @#@#; ...