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