1: <?php
 2:  3:  4:  5:  6:  7: 
 8: 
 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 
20: function smarty_outputfilter_trimwhitespace($source)
21: {
22:     $store = array();
23:     $_store = 0;
24:     $_offset = 0;
25: 
26:     
27:     $source = preg_replace("/\015\012|\015|\012/", "\n", $source);
28: 
29:     
30:     if (preg_match_all('#<!--\[[^\]]+\]>.*?<!\[[^\]]+\]-->#is', $source, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
31:         foreach ($matches as $match) {
32:             $store[] = $match[0][0];
33:             $_length = strlen($match[0][0]);
34:             $replace = '@!@SMARTY:' . $_store . ':SMARTY@!@';
35:             $source = substr_replace($source, $replace, $match[0][1] - $_offset, $_length);
36: 
37:             $_offset += $_length - strlen($replace);
38:             $_store ++;
39:         }
40:     }
41: 
42:     
43:     
44:     $source = preg_replace('#<!--.*?-->#ms', '', $source);
45: 
46:     
47:     $_offset = 0;
48:     if (preg_match_all('#<(script|pre|textarea)[^>]*>.*?</\\1>#is', $source, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
49:         foreach ($matches as $match) {
50:             $store[] = $match[0][0];
51:             $_length = strlen($match[0][0]);
52:             $replace = '@!@SMARTY:' . $_store . ':SMARTY@!@';
53:             $source = substr_replace($source, $replace, $match[0][1] - $_offset, $_length);
54: 
55:             $_offset += $_length - strlen($replace);
56:             $_store ++;
57:         }
58:     }
59: 
60:     $expressions = array(
61:         
62:         
63:         '#(:SMARTY@!@|>)\s+(?=@!@SMARTY:|<)#s'                            => '\1 \2',
64:         
65:         '#(([a-z0-9]\s*=\s*(["\'])[^\3]*?\3)|<[a-z0-9_]+)\s+([a-z/>])#is' => '\1 \4',
66:         
67:         
68:         '#^\s+<#Ss'                                                       => '<',
69:         '#>\s+$#Ss'                                                       => '>',
70:     );
71: 
72:     $source = preg_replace(array_keys($expressions), array_values($expressions), $source);
73:     
74:     
75:     
76: 
77:     $_offset = 0;
78:     if (preg_match_all('#@!@SMARTY:([0-9]+):SMARTY@!@#is', $source, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
79:         foreach ($matches as $match) {
80:             $_length = strlen($match[0][0]);
81:             $replace = $store[$match[1][0]];
82:             $source = substr_replace($source, $replace, $match[0][1] + $_offset, $_length);
83: 
84:             $_offset += strlen($replace) - $_length;
85:             $_store ++;
86:         }
87:     }
88: 
89:     return $source;
90: }
91: