home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / block.textformat.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  2.0 KB  |  83 lines

  1. <?php
  2. /**
  3.  * Smarty plugin
  4.  * @package Smarty
  5.  * @subpackage plugins
  6.  */
  7.  
  8. /**
  9.  * Smarty {textformat}{/textformat} block plugin
  10.  *
  11.  * Type:     block function<br>
  12.  * Name:     textformat<br>
  13.  * Purpose:  format text a certain way with preset styles
  14.  *           or custom wrap/indent settings<br>
  15.  * @link http://smarty.php.net/manual/en/language.function.textformat.php {textformat}
  16.  *       (Smarty online manual)
  17.  * @param array
  18.  * <pre>
  19.  * Params:   style: string (email)
  20.  *           indent: integer (0)
  21.  *           wrap: integer (80)
  22.  *           wrap_char string ("\n")
  23.  *           indent_char: string (" ")
  24.  *           wrap_boundary: boolean (true)
  25.  * </pre>
  26.  * @param string contents of the block
  27.  * @param Smarty clever simulation of a method
  28.  * @return string string $content re-formatted
  29.  */
  30. function smarty_block_textformat($params, $content, &$this)
  31. {
  32.     $style = null;
  33.     $indent = 0;
  34.     $indent_first = 0;
  35.     $indent_char = ' ';
  36.     $wrap = 80;
  37.     $wrap_char = "\n";
  38.     $wrap_cut = false;
  39.     $assign = null;
  40.     
  41.     if($content == null) {
  42.         return true;
  43.     }
  44.  
  45.     extract($params);
  46.  
  47.     if($style == 'email') {
  48.         $wrap = 72;
  49.     }    
  50.     
  51.     // split into paragraphs    
  52.     $paragraphs = preg_split('![\r\n][\r\n]!',$content);
  53.     
  54.     foreach($paragraphs as $paragraph) {
  55.         if($paragraph == '') {
  56.             continue;
  57.         }
  58.         // convert mult. spaces & special chars to single space
  59.         $paragraph = preg_replace(array('!\s+!','!(^\s+)|(\s+$)!'),array(' ',''),$paragraph);
  60.         // indent first line
  61.         if($indent_first > 0) {
  62.             $paragraph = str_repeat($indent_char,$indent_first) . $paragraph;
  63.         }
  64.         // wordwrap sentences
  65.         $paragraph = wordwrap($paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
  66.         // indent lines
  67.         if($indent > 0) {
  68.             $paragraph = preg_replace('!^!m',str_repeat($indent_char,$indent),$paragraph);
  69.         }
  70.         $output .= $paragraph . $wrap_char . $wrap_char;
  71.     }
  72.                 
  73.     if($assign != null) {
  74.         $this->assign($assign,$output);
  75.     } else {
  76.         return $output;
  77.     }
  78. }
  79.  
  80. /* vim: set expandtab: */
  81.  
  82. ?>
  83.