5 * @subpackage plugins
\r
9 * Smarty trimwhitespace outputfilter plugin
\r
11 * File: outputfilter.trimwhitespace.php<br>
\r
12 * Type: outputfilter<br>
\r
13 * Name: trimwhitespace<br>
\r
14 * Date: Jan 25, 2003<br>
\r
15 * Purpose: trim leading white space and blank lines from
\r
16 * template source after it gets interpreted, cleaning
\r
17 * up code and saving bandwidth. Does not affect
\r
18 * <<PRE>></PRE> and <SCRIPT></SCRIPT> blocks.<br>
\r
19 * Install: Drop into the plugin directory, call
\r
20 * <code>$smarty->load_filter('output','trimwhitespace');</code>
\r
22 * @author Monte Ohrt <monte@ispi.net>
\r
23 * @author Contributions from Lars Noschinski <lars@usenet.noschinski.de>
\r
28 function smarty_outputfilter_trimwhitespace($source, &$smarty)
\r
30 // Pull out the script blocks
\r
31 preg_match_all("!<script[^>]+>.*?</script>!is", $source, $match);
\r
32 $_script_blocks = $match[0];
\r
33 $source = preg_replace("!<script[^>]+>.*?</script>!is",
\r
34 '@@@SMARTY:TRIM:SCRIPT@@@', $source);
\r
36 // Pull out the pre blocks
\r
37 preg_match_all("!<pre>.*?</pre>!is", $source, $match);
\r
38 $_pre_blocks = $match[0];
\r
39 $source = preg_replace("!<pre>.*?</pre>!is",
\r
40 '@@@SMARTY:TRIM:PRE@@@', $source);
\r
42 // Pull out the textarea blocks
\r
43 preg_match_all("!<textarea[^>]+>.*?</textarea>!is", $source, $match);
\r
44 $_textarea_blocks = $match[0];
\r
45 $source = preg_replace("!<textarea[^>]+>.*?</textarea>!is",
\r
46 '@@@SMARTY:TRIM:TEXTAREA@@@', $source);
\r
48 // remove all leading spaces, tabs and carriage returns NOT
\r
49 // preceeded by a php close tag.
\r
50 $source = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source));
\r
52 // replace script blocks
\r
53 smarty_outputfilter_trimwhitespace_replace("@@@SMARTY:TRIM:SCRIPT@@@",$_script_blocks, $source);
\r
55 // replace pre blocks
\r
56 smarty_outputfilter_trimwhitespace_replace("@@@SMARTY:TRIM:PRE@@@",$_pre_blocks, $source);
\r
58 // replace textarea blocks
\r
59 smarty_outputfilter_trimwhitespace_replace("@@@SMARTY:TRIM:TEXTAREA@@@",$_textarea_blocks, $source);
\r
64 function smarty_outputfilter_trimwhitespace_replace($search_str, $replace, &$subject) {
\r
65 $_len = strlen($search_str);
\r
67 for ($_i=0, $_count=count($replace); $_i<$_count; $_i++)
\r
68 if (($_pos=strpos($subject, $search_str, $_pos))!==false)
\r
69 $subject = substr_replace($subject, $replace[$_i], $_pos, $_len);
\r