6 * @subpackage PluginsFilter
10 * Smarty trimwhitespace outputfilter plugin
12 * File: outputfilter.trimwhitespace.php<br>
13 * Type: outputfilter<br>
14 * Name: trimwhitespace<br>
15 * Date: Jan 25, 2003<br>
16 * Purpose: trim leading white space and blank lines from
17 * template source after it gets interpreted, cleaning
18 * up code and saving bandwidth. Does not affect
19 * <<PRE>></PRE> and <SCRIPT></SCRIPT> blocks.<br>
20 * Install: Drop into the plugin directory, call
21 * <code>$smarty->load_filter('output','trimwhitespace');</code>
23 * @author Monte Ohrt <monte at ohrt dot com>
24 * @author Contributions from Lars Noschinski <lars@usenet.noschinski.de>
26 * @param string $source input string
27 * @param object &$smarty Smarty object
28 * @return string filtered output
30 function smarty_outputfilter_trimwhitespace($source, $smarty)
32 // Pull out the script blocks
33 preg_match_all("!<script[^>]*?>.*?</script>!is", $source, $match);
34 $_script_blocks = $match[0];
35 $source = preg_replace("!<script[^>]*?>.*?</script>!is",
36 '@@@SMARTY:TRIM:SCRIPT@@@', $source);
38 // Pull out the pre blocks
39 preg_match_all("!<pre[^>]*?>.*?</pre>!is", $source, $match);
40 $_pre_blocks = $match[0];
41 $source = preg_replace("!<pre[^>]*?>.*?</pre>!is",
42 '@@@SMARTY:TRIM:PRE@@@', $source);
44 // Pull out the textarea blocks
45 preg_match_all("!<textarea[^>]*?>.*?</textarea>!is", $source, $match);
46 $_textarea_blocks = $match[0];
47 $source = preg_replace("!<textarea[^>]*?>.*?</textarea>!is",
48 '@@@SMARTY:TRIM:TEXTAREA@@@', $source);
50 // remove all leading spaces, tabs and carriage returns NOT
51 // preceeded by a php close tag.
52 $source = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source));
54 // replace textarea blocks
55 smarty_outputfilter_trimwhitespace_replace("@@@SMARTY:TRIM:TEXTAREA@@@",$_textarea_blocks, $source);
58 smarty_outputfilter_trimwhitespace_replace("@@@SMARTY:TRIM:PRE@@@",$_pre_blocks, $source);
60 // replace script blocks
61 smarty_outputfilter_trimwhitespace_replace("@@@SMARTY:TRIM:SCRIPT@@@",$_script_blocks, $source);
66 function smarty_outputfilter_trimwhitespace_replace($search_str, $replace, &$subject) {
67 $_len = strlen($search_str);
69 for ($_i=0, $_count=count($replace); $_i<$_count; $_i++)
70 if (($_pos=strpos($subject, $search_str, $_pos))!==false)
71 $subject = substr_replace($subject, $replace[$_i], $_pos, $_len);