1: <?php
2: /**
3: * The Horde_Text_Filter_Tabs2spaces:: converts tabs into spaces.
4: *
5: * TODO: parameters (breakchar, tabstop)
6: *
7: * Copyright 2004-2012 Horde LLC (http://www.horde.org/)
8: *
9: * See the enclosed file COPYING for license information (LGPL). If you
10: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
11: *
12: * @author Chuck Hagenbuch <chuck@horde.org>
13: * @category Horde
14: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
15: * @package Text_Filter
16: */
17: class Horde_Text_Filter_Tabs2spaces extends Horde_Text_Filter_Base
18: {
19: /**
20: * Filter parameters.
21: *
22: * @var array
23: */
24: protected $_params = array(
25: 'breakchar' => "\n",
26: 'tabstop' => 8
27: );
28:
29: /**
30: * Executes any code necessary before applying the filter patterns.
31: *
32: * @param string $text The text before the filtering.
33: *
34: * @return string The modified text.
35: */
36: public function preProcess($text)
37: {
38: $lines = explode($this->_params['breakchar'], $text);
39: for ($i = 0, $l = count($lines); $i < $l; ++$i) {
40: while (($pos = strpos($lines[$i], "\t")) !== false) {
41: $new_str = str_repeat(' ', $this->_params['tabstop'] - ($pos % $this->_params['tabstop']));
42: $lines[$i] = substr_replace($lines[$i], $new_str, $pos, 1);
43: }
44: }
45: return implode("\n", $lines);
46: }
47:
48: }
49: