插件1:文本换行

<?php // Plug-in 1: Wrap Text

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

$text = "Treats of the place where Oliver Twist was born, and of the circumstances attending his birth.\nAmong other public buildings in a certain town, which for many reasons it will be prudent to refrain from mentioning, and to which I will assign no fictitious name, there is one anciently common to most towns, great or small: to wit, a workhouse; and in this workhouse was born; on a day and date which I need not trouble myself to repeat, inasmuch as it can be of no possible consequence to the reader, in this stage of the business at all events; the item of mortality whose name is prefixed to the head of this chapter.\nFor a long time after it was ushered into this world of sorrow and trouble, by the parish surgeon, it remained a matter of considerable doubt whether the child would survive to bear any name at all; in which case it is somewhat more than probable that these memoirs would never have appeared; or, if they had, that being comprised within a couple of pages, they would have possessed the inestimable merit of being the most concise and faithful specimen of biography, extant in the literature of any age or country.";

echo "<font face='Courier New' size='2'>";
echo PIPHP_WrapText($text, 71, 5);

function PIPHP_WrapText($text, $width, $indent)
{
   // Plug-in 1: Wrap Text
   //
   // This plug-in takes a string variable containing any
   // text and then adds <br /> and   tags in the right
   // places to make the text wrap and indent para starts.
   // The arguments required are:
   //
   //    $text: Text to be modified
   //    $width: Number of characters art which to wrap
   //    $indent: Number of chars to indent para start

   $wrapped    = "";
   $paragraphs = explode("\n", $text);

   foreach($paragraphs as $paragraph)
   {
      if ($indent > 0) $wrapped .= str_repeat(" ", $indent);
      
      $words = explode(" ", $paragraph);
      $len   = $indent;

      foreach($words as $word)
      {
         $wlen = strlen($word);

         if (($len + $wlen) < $width)
         {
            $wrapped .= "$word ";
            $len     += $wlen + 1;
         }
         else
         {
            $wrapped = trim($wrapped);
            $wrapped .= "<br />\n$word ";
            $len     =  $wlen;
         }
      }

      $wrapped = trim($wrapped);
      $wrapped .= "<br />\n";
   }

   return $wrapped;
}

?>

 1.插件说明:

    插件1接受一个任意的字符串变量,在合适的位置插入<br/>和"&nbsp",并且设置段首缩进一定距离。它需要三个参数:

$text:需要换行的字符串变量。

$width:整数,表示在这个位置强制文本换行。

$indent:整数,表示段首缩进的字符个数

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值