PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/framework/vendor/smarty3/lib/libs/plugins/outputfilter.trimwhitespace.php

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