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.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  20.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  21.  * @license    http://dwoo.org/LICENSE   Modified BSD License
  22.  * @link       http://dwoo.org/
  23.  * @version    1.0.0
  24.  * @date       2008-10-23
  25.  * @package    Dwoo
  26.  */
  27. {
  28.     protected $wrap;
  29.     protected $wrapChar;
  30.     protected $wrapCut;
  31.     protected $indent;
  32.     protected $indChar;
  33.     protected $indFirst;
  34.     protected $assign;
  35.  
  36.     public function init($wrap=80, $wrap_char="\r\n", $wrap_cut=false, $indent=0, $indent_char=" ", $indent_first=0, $style="", $assign="")
  37.     {
  38.         if ($indent_char === 'tab') {
  39.             $indent_char = "\t";
  40.         }
  41.  
  42.         switch($style) {
  43.  
  44.         case 'email':
  45.             $wrap = 72;
  46.             $indent_first = 0;
  47.             break;
  48.         case 'html':
  49.             $wrap_char = '<br />';
  50.             $indent_char = $indent_char == "\t" ? '&nbsp;&nbsp;&nbsp;&nbsp;':'&nbsp;';
  51.             break;
  52.  
  53.         }
  54.  
  55.         $this->wrap = (int) $wrap;
  56.         $this->wrapChar = (string) $wrap_char;
  57.         $this->wrapCut = (bool) $wrap_cut;
  58.         $this->indent = (int) $indent;
  59.         $this->indChar = (string) $indent_char;
  60.         $this->indFirst = (int) $indent_first + $this->indent;
  61.         $this->assign = (string) $assign;
  62.     }
  63.  
  64.     public function process()
  65.     {
  66.         // gets paragraphs
  67.         $pgs = explode("\n", str_replace(array("\r\n", "\r"), "\n", $this->buffer));
  68.  
  69.         while (list($i,) = each($pgs)) {
  70.             if (empty($pgs[$i])) {
  71.                 continue;
  72.             }
  73.  
  74.             // removes line breaks and extensive white space
  75.             $pgs[$i] = preg_replace(array('#\s+#', '#^\s*(.+?)\s*$#m'), array(' ', '$1'), str_replace("\n", '', $pgs[$i]));
  76.  
  77.             // wordwraps + indents lines
  78.             $pgs[$i] = str_repeat($this->indChar, $this->indFirst) .
  79.                        wordwrap(
  80.                             $pgs[$i],
  81.                             max($this->wrap - $this->indent, 1),
  82.                             $this->wrapChar . str_repeat($this->indChar, $this->indent),
  83.                             $this->wrapCut
  84.                     );
  85.         }
  86.  
  87.         if ($this->assign !== '') {
  88.             $this->dwoo->assignInScope(implode($this->wrapChar . $this->wrapChar, $pgs), $this->assign);
  89.         } else {
  90.             return implode($this->wrapChar . $this->wrapChar, $pgs);
  91.         }
  92.     }
  93. }

Documentation generated on Sun, 07 Feb 2010 17:53:59 +0000 by phpDocumentor 1.4.0