插件7:自动断字

<?php // Plug-in 7: Text Truncate

// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

$text  = "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to heaven.";

echo "$text<br /><br />";
echo PIPHP_TextTruncate($text, 28, "…") . "<br />";
echo PIPHP_TextTruncate($text, 50, ":-")       . "<br />";
echo PIPHP_TextTruncate($text, 90, " --")      . "<br />";

function PIPHP_textTruncate($text, $max, $symbol)
{
   // Plug-in 7: Text Truncate
   //
   // This plug-in takes a string variable containing any
   // text and then smartly truncates it to no more than
   // $max characters starting at the previous space.
   // The arguments required are:
   //
   //    $text: Text to be modified
   //    $max:  Maximum number of characters

   $temp = substr($text, 0, $max);
   $last = strrpos($temp, " ");
   $temp = substr($temp, 0, $last);
   $temp = preg_replace("/([^\w])$/", "", $temp);
   return "$temp$symbol";
}

?>

1. 插件说明:

插件7有三个参数。第一个参数表示需要处理的文本,第二个参数表示在新字符串里允许的最大个数,第三个参数是一个符号或字符串,它添加在截断文本后面,说明已经执行了阶段操作。它的三个参数分别是:

$text 字符串变量,包含需要处理的文本

$max 数值变量,表示最大的字符个数

$symbol 字符串变量,表示添加在新文本的后面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值