psr12 fixes for new PHP_CodeSniffer (#4795)
[openemr.git] / portal / patient / fwk / libs / savant / Savant3 / resources / Savant3_Filter_trimwhitespace.php
blobdb9d8053486f9503660340828f21f267c32e9410
1 <?php
3 /**
5 * Filter to remove extra white space within the text.
7 * @package Savant3
9 * @author Monte Ohrt <monte@ispi.net>
11 * @author Contributions from Lars Noschinski <lars@usenet.noschinski.de>
13 * @author Converted to a Savant3 filter by Paul M. Jones <pmjones@ciaweb.net>
15 * @license http://www.gnu.org/copyleft/lesser.html LGPL
17 * @version $Id: Savant3_Filter_trimwhitespace.php,v 1.4 2005/05/29 15:27:07 pmjones Exp $
21 /**
23 * Filter to remove extra white space within the text.
25 * @package Savant3
27 * @author Monte Ohrt <monte@ispi.net>
29 * @author Contributions from Lars Noschinski <lars@usenet.noschinski.de>
31 * @author Converted to a Savant3 filter by Paul M. Jones <pmjones@ciaweb.net>
35 class Savant3_Filter_trimwhitespace extends Savant3_Filter
37 /**
39 * Removes extra white space within the text.
41 * Trim leading white space and blank lines from template source
42 * after it gets interpreted, cleaning up code and saving bandwidth.
43 * Does not affect <pre></pre>, <script></script>, or
44 * <textarea></textarea> blocks.
46 * @access public
48 * @param string $buffer
49 * The source text to be filtered.
51 * @return string The filtered text.
54 public static function filter($buffer)
56 // Pull out the script blocks
57 preg_match_all("!<script[^>]+>.*?</script>!is", $buffer, $match);
58 $script_blocks = $match [0];
59 $buffer = preg_replace("!<script[^>]+>.*?</script>!is", '@@@SAVANT:TRIM:SCRIPT@@@', $buffer);
61 // Pull out the pre blocks
62 preg_match_all("!<pre[^>]*>.*?</pre>!is", $buffer, $match);
63 $pre_blocks = $match [0];
64 $buffer = preg_replace("!<pre[^>]*>.*?</pre>!is", '@@@SAVANT:TRIM:PRE@@@', $buffer);
66 // Pull out the textarea blocks
67 preg_match_all("!<textarea[^>]+>.*?</textarea>!is", $buffer, $match);
68 $textarea_blocks = $match [0];
69 $buffer = preg_replace("!<textarea[^>]+>.*?</textarea>!is", '@@@SAVANT:TRIM:TEXTAREA@@@', $buffer);
71 // remove all leading spaces, tabs and carriage returns NOT
72 // preceeded by a php close tag.
73 $buffer = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $buffer));
75 // replace script blocks
76 Savant3_Filter_trimwhitespace::replace("@@@SAVANT:TRIM:SCRIPT@@@", $script_blocks, $buffer);
78 // replace pre blocks
79 Savant3_Filter_trimwhitespace::replace("@@@SAVANT:TRIM:PRE@@@", $pre_blocks, $buffer);
81 // replace textarea blocks
82 Savant3_Filter_trimwhitespace::replace("@@@SAVANT:TRIM:TEXTAREA@@@", $textarea_blocks, $buffer);
84 return $buffer;
87 /**
89 * Does a simple search-and-replace on the source text.
91 * @access protected
93 * @param string $search
94 * The string to search for.
96 * @param string $replace
97 * Replace with this text.
99 * @param
100 * string &$buffer The source text.
102 * @return string The text after search-and-replace.
105 protected static function replace($search, $replace, &$buffer)
107 $len = strlen($search);
108 $pos = 0;
109 $count = count($replace);
111 for ($i = 0; $i < $count; $i++) {
112 // does the search-string exist in the buffer?
113 $pos = strpos($buffer, $search, $pos);
114 if ($pos !== false) {
115 // replace the search-string
116 $buffer = substr_replace($buffer, $replace [$i], $pos, $len);
117 } else {
118 break;