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 jk1986_checksql() { $bad_str = "and|select|update|'|delete|insert|*"; $ba...

php读取xml实例代码

php文件 复制代码 代码如下: <?php class xpathExtension{ public static function getNodes($domDoc, $xpa...

thinkphp 验证码 的使用小结

 thinkphp中的验证码是可以直接调用的,非常方便,我们看一下 Think 文件夹下 有一个名为verify.class.php的文件    首先 我们...

PHP5.4中json_encode中文转码的变化小结

在php5.4以前做json_encode的时候中文会被unicode编码,中文都会被编码,变成不可读的,类似“\u***”的格式,还会在一定程度上增加传输的数据量。 例如: 复制代码...

php cookie的操作实现代码(登录)

第一个文件login_frm.php这个是登录窗口 代码 复制代码 代码如下: <html> <head> <meta http-equiv="Conten...