XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
block.textformat.php
Go to the documentation of this file.
1 <?php
31 function smarty_block_textformat($params, $content, &$smarty)
32 {
33  if (is_null($content)) {
34  return;
35  }
36 
37  $style = null;
38  $indent = 0;
39  $indent_first = 0;
40  $indent_char = ' ';
41  $wrap = 80;
42  $wrap_char = "\n";
43  $wrap_cut = false;
44  $assign = null;
45 
46  foreach ($params as $_key => $_val) {
47  switch ($_key) {
48  case 'style':
49  case 'indent_char':
50  case 'wrap_char':
51  case 'assign':
52  $$_key = (string)$_val;
53  break;
54 
55  case 'indent':
56  case 'indent_first':
57  case 'wrap':
58  $$_key = (int)$_val;
59  break;
60 
61  case 'wrap_cut':
62  $$_key = (bool)$_val;
63  break;
64 
65  default:
66  $smarty->trigger_error("textformat: unknown attribute '$_key'");
67  }
68  }
69 
70  if ($style == 'email') {
71  $wrap = 72;
72  }
73 
74  // split into paragraphs
75  $_paragraphs = preg_split('![\r\n][\r\n]!',$content);
76  $_output = '';
77 
78  for($_x = 0, $_y = count($_paragraphs); $_x < $_y; $_x++) {
79  if ($_paragraphs[$_x] == '') {
80  continue;
81  }
82  // convert mult. spaces & special chars to single space
83  $_paragraphs[$_x] = preg_replace(array('!\s+!','!(^\s+)|(\s+$)!'), array(' ',''), $_paragraphs[$_x]);
84  // indent first line
85  if($indent_first > 0) {
86  $_paragraphs[$_x] = str_repeat($indent_char, $indent_first) . $_paragraphs[$_x];
87  }
88  // wordwrap sentences
89  $_paragraphs[$_x] = wordwrap($_paragraphs[$_x], $wrap - $indent, $wrap_char, $wrap_cut);
90  // indent lines
91  if($indent > 0) {
92  $_paragraphs[$_x] = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraphs[$_x]);
93  }
94  }
95  $_output = implode($wrap_char . $wrap_char, $_paragraphs);
96 
97  return $assign ? $smarty->assign($assign, $_output) : $_output;
98 
99 }
100 
101 /* vim: set expandtab: */
102 
103 ?>