1: <?php
2: /**
3: * The space2html filter converts horizontal whitespace to HTML code.
4: *
5: * Parameters:
6: * <pre>
7: * encode -- HTML encode the text? Defaults to false.
8: * charset -- Charset of the text. Defaults to ISO-8859-1.
9: * encode_all -- Replace all spaces with ? Defaults to false.
10: * </pre>
11: *
12: * Copyright 2001-2012 Horde LLC (http://www.horde.org/)
13: *
14: * See the enclosed file COPYING for license information (LGPL). If you
15: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
16: *
17: * @author Jan Schneider <jan@horde.org>
18: * @author Mathieu Arnold <mat@mat.cc>
19: * @category Horde
20: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
21: * @package Text_Filter
22: */
23: class Horde_Text_Filter_Space2html extends Horde_Text_Filter_Base
24: {
25: /**
26: * Filter parameters.
27: *
28: * @var array
29: */
30: protected $_params = array(
31: 'charset' => 'ISO-8859-1',
32: 'encode' => false,
33: 'encode_all' => false
34: );
35:
36: /**
37: * Executes any code necessary before applying the filter patterns.
38: *
39: * @param string $text The text before the filtering.
40: *
41: * @return string The modified text.
42: */
43: public function preProcess($text)
44: {
45: if ($this->_params['encode']) {
46: $text = @htmlspecialchars($text, ENT_COMPAT, $this->_params['charset']);
47: }
48: return $text;
49: }
50:
51: /**
52: * Returns a hash with replace patterns.
53: *
54: * @return array Patterns hash.
55: */
56: public function getPatterns()
57: {
58: return array(
59: 'replace' => array(
60: "\t" => ' ',
61: ' ' => ' '
62: )
63: );
64: }
65:
66: /**
67: * Executes any code necessaray after applying the filter patterns.
68: *
69: * @param string $text The text after the filtering.
70: *
71: * @return string The modified text.
72: */
73: public function postProcess($text)
74: {
75: $text = str_replace(' ', ' ', $text);
76: if ($this->_params['encode_all']) {
77: $text = str_replace(' ', ' ', $text);
78: }
79: return $text;
80: }
81:
82: }
83: