Source for file textformat.php

Documentation is available at textformat.php

  1. <?php
  2.  
  3. /**
  4.  * Formats a string to the given format, you can wrap lines at a certain
  5.  * length and indent them
  6.  * <pre>
  7.  *  * wrap : maximum line length
  8.  *  * wrap_char : the character(s) to use to break the line
  9.  *  * wrap_cut : if true, the words that are longer than $wrap are cut instead of overflowing
  10.  *  * indent : amount of $indent_char to insert before every line
  11.  *  * indent_char : character(s) to insert before every line
  12.  *  * indent_first : amount of additional $indent_char to insert before the first line of each paragraphs
  13.  *  * style : some predefined formatting styles that set up every required variables, can be "email" or "html"
  14.  *  * assign : if set, the formatted text is assigned to that variable instead of being output
  15.  * </pre>
  16.  * This software is provided 'as-is', without any express or implied warranty.
  17.  * In no event will the authors be held liable for any damages arising from the use of this software.
  18.  *
  19.  * This file is released under the LGPL
  20.  * "GNU Lesser General Public License"
  21.  * More information can be found here:
  22.  * {@link http://www.gnu.org/copyleft/lesser.html}
  23.  *
  24.  * @author     Jordi Boggiano <[email protected]>
  25.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  26.  * @license    http://www.gnu.org/copyleft/lesser.html  GNU Lesser General Public License
  27.  * @link       http://dwoo.org/
  28.  * @version    0.9.1
  29.  * @date       2008-05-30
  30.  * @package    Dwoo
  31.  */
  32. {
  33.     protected $wrap;
  34.     protected $wrapChar;
  35.     protected $wrapCut;
  36.     protected $indent;
  37.     protected $indChar;
  38.     protected $indFirst;
  39.     protected $assign;
  40.  
  41.     public function init($wrap=80$wrap_char="\r\n"$wrap_cut=false$indent=0$indent_char=" "$indent_first=0$style=""$assign="")
  42.     {
  43.         if ($indent_char === 'tab'{
  44.             $indent_char "\t";
  45.         }
  46.  
  47.         switch($style{
  48.  
  49.         case 'email':
  50.             $wrap 72;
  51.             $indent_first 0;
  52.             break;
  53.         case 'html':
  54.             $wrap_char '<br />';
  55.             $indent_char $indent_char == "\t" '&nbsp;&nbsp;&nbsp;&nbsp;':'&nbsp;';
  56.             break;
  57.  
  58.         }
  59.  
  60.         $this->wrap = (int) $wrap;
  61.         $this->wrapChar = (string) $wrap_char;
  62.         $this->wrapCut = (bool) $wrap_cut;
  63.         $this->indent = (int) $indent;
  64.         $this->indChar = (string) $indent_char;
  65.         $this->indFirst = (int) $indent_first $this->indent;
  66.         $this->assign = (string) $assign;
  67.     }
  68.  
  69.     public function process()
  70.     {
  71.         // gets paragraphs
  72.         $pgs explode("\n"str_replace(array("\r\n""\r")"\n"$this->buffer));
  73.  
  74.         while (list($i,each($pgs)) {
  75.             if (empty($pgs[$i])) {
  76.                 continue;
  77.             }
  78.  
  79.             // removes line breaks and extensive white space
  80.             $pgs[$ipreg_replace(array('#\s+#''#^\s*(.+?)\s*$#m')array(' ''$1')str_replace("\n"''$pgs[$i]));
  81.  
  82.             // wordwraps + indents lines
  83.             $pgs[$istr_repeat($this->indChar$this->indFirst.
  84.                        wordwrap(
  85.                             $pgs[$i],
  86.                             max($this->wrap - $this->indent1),
  87.                             $this->wrapChar . str_repeat($this->indChar$this->indent),
  88.                             $this->wrapCut
  89.                     );
  90.         }
  91.  
  92.         if ($this->assign !== ''{
  93.             $this->dwoo->assignInScope(implode($this->wrapChar . $this->wrapChar$pgs)$this->assign);
  94.         else {
  95.             return implode($this->wrapChar . $this->wrapChar$pgs);
  96.         }
  97.     }
  98. }

Documentation generated on Sun, 03 Aug 2008 15:12:47 +0200 by phpDocumentor 1.4.0