216 | | // Closes open HTML tags. Not quite perfect... |
217 | | |
218 | | function closeTags($str = null) { |
219 | | // Function from http://episteme.arstechnica.com/eve/ubb.x/a/tpc/f/6330927813/m/139006252731/r/287008552731#287008552731 |
220 | | // (thanks Ageless for finding it) |
221 | | // Edited by Rob to better fit in with boinc's needs |
222 | | |
223 | | // List of tags to check $str for |
224 | | // TODO: Adapt to use the pre-existing array of tags above |
225 | | $tags = array('b', 'i', 'a', 'p', 'font[^>]?', 'strong', 'ul', 'li', 'pre', 'blockquote', 'u'); |
226 | | // Note on $tags - no br or img, as they have no closing tags - can we define this above? |
227 | | // Maybe define two arrays, those with closing tags and those without, and combine the |
228 | | // two of them for the standard HTML sanitizing function? |
229 | | |
230 | | // Don't do anything if the string is too short |
231 | | if (strlen($str) < 3) { |
232 | | return $str; |
233 | | } else { |
234 | | // Loop over $str and count the opening and closing for each tag in $tags |
235 | | foreach ($tags as $tag) { |
236 | | $m = array(); |
237 | | $o = preg_match_all("/<(".$tag.")>/", $str, $m); |
238 | | $c = substr_count($str, "</{$tag}>"); |
239 | | |
240 | | $open[$tag] = ($o < $c) ? $c - $o : 0; |
241 | | $close[$tag] = ($c < $o) ? $o - $c : 0; |
242 | | |
243 | | // Debuggin' |
244 | | //echo "<pre>Tag: {$tag}\nOpen: {$o}\nClose: {$c}\nOT: {$open[$tag]}\nCT: {$close[$tag]}</pre><hr />"; |
245 | | } |
246 | | |
247 | | // Prepend the return string with an opening tag as needed |
248 | | /* $pre = ''; ...uhh... doesn't work right |
249 | | |
250 | | foreach ($open as $tag => $cnt) { |
251 | | $pre .= ($cnt > 0) ? "<{$tag}>" : ''; |
252 | | } */ |
253 | | |
254 | | // Append the return string with a closing tag as needed |
255 | | $post = ''; |
256 | | |
257 | | foreach ($close as $tag => $cnt) { |
258 | | $post .= ($cnt > 0) ? "</{$tag}>" : ''; |
259 | | } |
260 | | |
261 | | return /*$pre.*/$str.$post; |
262 | | } |
263 | | } |
264 | | |