@@ -112,16 +112,19 @@ function substr2($s, $n1, $n2) {
112112function replace_pre_code ($ text , $ export ) {
113113 $ out = '' ;
114114 $ pos = 0 ;
115+
116+ // there maybe several. Scan the text
117+ //
115118 while (true ) {
119+ // find the first instance of either [code] or [pre]
120+ //
116121 $ n1 = strpos ($ text , '[code] ' , $ pos );
117122 $ n2 = strpos ($ text , '[pre] ' , $ pos );
118123 if ($ n1 === false && $ n2 === false ) {
119124 $ out .= substr ($ text , $ pos );
120125 break ;
121126 }
122- if ($ n1 === false ) $ n1 = 9999 ;
123- if ($ n2 === false ) $ n2 = 9999 ;
124- if ($ n1 < $ n2 ) {
127+ if ($ n2 === false || $ n1 < $ n2 ) {
125128 $ n = $ n1 ;
126129 $ tag = '[code] ' ;
127130 $ tag2 = '[/code] ' ;
@@ -130,17 +133,29 @@ function replace_pre_code($text, $export) {
130133 $ tag = '[pre] ' ;
131134 $ tag2 = '[/pre] ' ;
132135 }
136+ // output the part before that
137+ //
133138 $ out .= substr2 ($ text , $ pos , $ n );
134- $ n2 = strpos ($ text , $ tag2 , $ n );
139+
140+ // find the close tag
141+ //
135142 $ n += strlen ($ tag );
143+ $ n2 = strpos ($ text , $ tag2 , $ n );
144+
145+ // if none, output rest of string and we're done
146+ //
136147 if (!$ n2 ) {
137148 $ out .= substr ($ text , $ n );
138149 break ;
139150 }
151+
152+ // get the text between open and close tags, and sanitize it
153+ //
140154 $ x = substr2 ($ text , $ n , $ n2 );
141155 $ x = remove_br ($ x );
142156 $ x = htmlspecialchars ($ x , ENT_COMPAT , "UTF-8 " , false );
143157 $ x = str_replace ("[ " , "[ " , $ x );
158+
144159 if ($ export ) {
145160 if ($ tag == '[pre] ' ) {
146161 $ out .= "<pre> $ x</pre> " ;
@@ -150,6 +165,14 @@ function replace_pre_code($text, $export) {
150165 } else {
151166 $ out .= "<pre style= \"white-space:pre-wrap; \"> $ x</pre> " ;
152167 }
168+
169+ // check for overflow (debug, shouldn't happen)
170+ if (strlen ($ out ) > 2 *strlen ($ text )) {
171+ return "<P>REPLACE_PRE_CODE BUG<p>text:<p><pre> $ text</pre> \n" ;
172+ }
173+
174+ // move past the close tag
175+ //
153176 $ pos = $ n2 + strlen ($ tag2 );
154177 }
155178 return $ out ;
0 commit comments