]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/functions-formatting.php
Wordpress 2.0.2
[autoinstalls/wordpress.git] / wp-includes / functions-formatting.php
1 <?php
2
3 function wptexturize($text) {
4         $output = '';
5         // Capture tags and everything inside them
6         $textarr = preg_split("/(<.*>)/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
7         $stop = count($textarr); $next = true; // loop stuff
8         for ($i = 0; $i < $stop; $i++) {
9                 $curl = $textarr[$i];
10
11                 if (isset($curl{0}) && '<' != $curl{0} && $next) { // If it's not a tag
12                         $curl = str_replace('---', '&#8212;', $curl);
13                         $curl = str_replace(' -- ', ' &#8212; ', $curl);
14                         $curl = str_replace('--', '&#8211;', $curl);
15                         $curl = str_replace('xn&#8211;', 'xn--', $curl);
16                         $curl = str_replace('...', '&#8230;', $curl);
17                         $curl = str_replace('``', '&#8220;', $curl);
18
19                         // This is a hack, look at this more later. It works pretty well though.
20                         $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");
21                         $cockneyreplace = array("&#8217;tain&#8217;t","&#8217;twere","&#8217;twas","&#8217;tis","&#8217;twill","&#8217;til","&#8217;bout","&#8217;nuff","&#8217;round","&#8217;cause");
22                         $curl = str_replace($cockney, $cockneyreplace, $curl);
23
24                         $curl = preg_replace("/'s/", '&#8217;s', $curl);
25                         $curl = preg_replace("/'(\d\d(?:&#8217;|')?s)/", "&#8217;$1", $curl);
26                         $curl = preg_replace('/(\s|\A|")\'/', '$1&#8216;', $curl);
27                         $curl = preg_replace('/(\d+)"/', '$1&#8243;', $curl);
28                         $curl = preg_replace("/(\d+)'/", '$1&#8242;', $curl);
29                         $curl = preg_replace("/(\S)'([^'\s])/", "$1&#8217;$2", $curl);
30                         $curl = preg_replace('/(\s|\A)"(?!\s)/', '$1&#8220;$2', $curl);
31                         $curl = preg_replace('/"(\s|\S|\Z)/', '&#8221;$1', $curl);
32                         $curl = preg_replace("/'([\s.]|\Z)/", '&#8217;$1', $curl);
33                         $curl = preg_replace("/ \(tm\)/i", ' &#8482;', $curl);
34                         $curl = str_replace("''", '&#8221;', $curl);
35                         
36                         $curl = preg_replace('/(\d+)x(\d+)/', "$1&#215;$2", $curl);
37
38                 } elseif (strstr($curl, '<code') || strstr($curl, '<pre') || strstr($curl, '<kbd' || strstr($curl, '<style') || strstr($curl, '<script'))) {
39                         // strstr is fast
40                         $next = false;
41                 } else {
42                         $next = true;
43                 }
44                 $curl = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/', '&#038;$1', $curl);
45                 $output .= $curl;
46         }
47         return $output;
48 }
49
50 function clean_pre($text) {
51         $text = str_replace('<br />', '', $text);
52         $text = str_replace('<p>', "\n", $text);
53         $text = str_replace('</p>', '', $text);
54         return $text;
55 }
56
57 function wpautop($pee, $br = 1) {
58         $pee = $pee . "\n"; // just to make things a little easier, pad the end
59         $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
60         // Space things out a little
61         $pee = preg_replace('!(<(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)!', "\n$1", $pee); 
62         $pee = preg_replace('!(</(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])>)!', "$1\n\n", $pee);
63         $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines 
64         $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
65         $pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $pee); // make paragraphs, including one at the end 
66         $pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace 
67         $pee = preg_replace('!<p>\s*(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|hr|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
68         $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
69         $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
70         $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
71         $pee = preg_replace('!<p>\s*(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|hr|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)!', "$1", $pee);
72         $pee = preg_replace('!(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)\s*</p>!', "$1", $pee); 
73         if ($br) $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
74         $pee = preg_replace('!(</?(?:table|thead|tfoot|caption|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)\s*<br />!', "$1", $pee);
75         $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)!', '$1', $pee);
76         $pee = preg_replace('!(<pre.*?>)(.*?)</pre>!ise', " stripslashes('$1') .  stripslashes(clean_pre('$2'))  . '</pre>' ", $pee);
77         
78         return $pee; 
79 }
80
81
82 function seems_utf8($Str) { # by bmorel at ssi dot fr
83         for ($i=0; $i<strlen($Str); $i++) {
84                 if (ord($Str[$i]) < 0x80) continue; # 0bbbbbbb
85                 elseif ((ord($Str[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
86                 elseif ((ord($Str[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
87                 elseif ((ord($Str[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb
88                 elseif ((ord($Str[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb
89                 elseif ((ord($Str[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
90                 else return false; # Does not match any model
91                 for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
92                         if ((++$i == strlen($Str)) || ((ord($Str[$i]) & 0xC0) != 0x80))
93                         return false;
94                 }
95         }
96         return true;
97 }
98
99 function wp_specialchars( $text, $quotes = 0 ) {
100         // Like htmlspecialchars except don't double-encode HTML entities
101         $text = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/', '&#038;$1', $text);-
102         $text = str_replace('<', '&lt;', $text);
103         $text = str_replace('>', '&gt;', $text);
104         if ( $quotes ) {
105                 $text = str_replace('"', '&quot;', $text);
106                 $text = str_replace("'", '&#039;', $text);
107         }
108         return $text;
109 }
110
111 function utf8_uri_encode( $utf8_string ) {
112   $unicode = '';        
113   $values = array();
114   $num_octets = 1;
115         
116   for ($i = 0; $i < strlen( $utf8_string ); $i++ ) {
117
118     $value = ord( $utf8_string[ $i ] );
119             
120     if ( $value < 128 ) {
121       $unicode .= chr($value);
122     } else {
123       if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3;
124                 
125       $values[] = $value;
126       
127       if ( count( $values ) == $num_octets ) {
128         if ($num_octets == 3) {
129           $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]);
130         } else {
131           $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]);
132         }
133
134         $values = array();
135         $num_octets = 1;
136       }
137     }
138   }
139
140   return $unicode;    
141 }
142
143 function remove_accents($string) {
144         if (seems_utf8($string)) {
145                 $chars = array(
146                 // Decompositions for Latin-1 Supplement
147                 chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
148                 chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
149                 chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
150                 chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
151                 chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
152                 chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
153                 chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
154                 chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
155                 chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
156                 chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
157                 chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
158                 chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
159                 chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
160                 chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
161                 chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
162                 chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
163                 chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
164                 chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
165                 chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
166                 chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
167                 chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
168                 chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
169                 chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
170                 chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
171                 chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
172                 chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
173                 chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
174                 chr(195).chr(191) => 'y',
175                 // Decompositions for Latin Extended-A
176                 chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
177                 chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
178                 chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
179                 chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
180                 chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
181                 chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
182                 chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
183                 chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
184                 chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
185                 chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
186                 chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
187                 chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
188                 chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
189                 chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
190                 chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
191                 chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
192                 chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
193                 chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
194                 chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
195                 chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
196                 chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
197                 chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
198                 chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
199                 chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
200                 chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
201                 chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
202                 chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
203                 chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
204                 chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
205                 chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
206                 chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
207                 chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
208                 chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
209                 chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
210                 chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
211                 chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
212                 chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
213                 chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
214                 chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
215                 chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
216                 chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
217                 chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
218                 chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
219                 chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
220                 chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
221                 chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
222                 chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
223                 chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
224                 chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
225                 chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
226                 chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
227                 chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
228                 chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
229                 chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
230                 chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
231                 chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
232                 chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
233                 chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
234                 chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
235                 chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
236                 chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
237                 chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
238                 chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
239                 chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
240                 // Euro Sign
241                 chr(226).chr(130).chr(172) => 'E');
242                 
243                 $string = strtr($string, $chars);
244         } else {
245                 // Assume ISO-8859-1 if not UTF-8
246                 $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
247                         .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
248                         .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
249                         .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
250                         .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
251                         .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
252                         .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
253                         .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
254                         .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
255                         .chr(252).chr(253).chr(255);
256
257                 $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
258
259                 $string = strtr($string, $chars['in'], $chars['out']);
260                 $double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
261                 $double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
262                 $string = str_replace($double_chars['in'], $double_chars['out'], $string);
263         }
264
265         return $string;
266 }
267
268 function sanitize_user( $username, $strict = false ) {
269         $raw_username = $username;
270         $username = strip_tags($username);
271         // Kill octets
272         $username = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '', $username);
273         $username = preg_replace('/&.+?;/', '', $username); // Kill entities
274
275         // If strict, reduce to ASCII for max portability.
276         if ( $strict )
277                 $username = preg_replace('|[^a-z0-9 _.-@]|i', '', $username);
278
279         return apply_filters('sanitize_user', $username, $raw_username, $strict);
280 }
281
282 function sanitize_title($title, $fallback_title = '') {
283         $title = strip_tags($title);
284         $title = apply_filters('sanitize_title', $title);
285
286         if (empty($title)) {
287                 $title = $fallback_title;
288         }
289
290         return $title;
291 }
292
293 function sanitize_title_with_dashes($title) {
294         $title = strip_tags($title);
295         // Preserve escaped octets.
296         $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
297         // Remove percent signs that are not part of an octet.
298         $title = str_replace('%', '', $title);
299         // Restore octets.
300         $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
301
302         $title = remove_accents($title);
303         if (seems_utf8($title)) {
304                 if (function_exists('mb_strtolower')) {
305                         $title = mb_strtolower($title, 'UTF-8');
306                 }
307                 $title = utf8_uri_encode($title);
308         }
309
310         $title = strtolower($title);
311         $title = preg_replace('/&.+?;/', '', $title); // kill entities
312         $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
313         $title = preg_replace('/\s+/', '-', $title);
314         $title = preg_replace('|-+|', '-', $title);
315         $title = trim($title, '-');
316
317         return $title;
318 }
319
320 function convert_chars($content, $flag = 'obsolete') { 
321         // Translation of invalid Unicode references range to valid range
322         $wp_htmltranswinuni = array(
323         '&#128;' => '&#8364;', // the Euro sign
324         '&#129;' => '',
325         '&#130;' => '&#8218;', // these are Windows CP1252 specific characters
326         '&#131;' => '&#402;',  // they would look weird on non-Windows browsers
327         '&#132;' => '&#8222;',
328         '&#133;' => '&#8230;',
329         '&#134;' => '&#8224;',
330         '&#135;' => '&#8225;',
331         '&#136;' => '&#710;',
332         '&#137;' => '&#8240;',
333         '&#138;' => '&#352;',
334         '&#139;' => '&#8249;',
335         '&#140;' => '&#338;',
336         '&#141;' => '',
337         '&#142;' => '&#382;',
338         '&#143;' => '',
339         '&#144;' => '',
340         '&#145;' => '&#8216;',
341         '&#146;' => '&#8217;',
342         '&#147;' => '&#8220;',
343         '&#148;' => '&#8221;',
344         '&#149;' => '&#8226;',
345         '&#150;' => '&#8211;',
346         '&#151;' => '&#8212;',
347         '&#152;' => '&#732;',
348         '&#153;' => '&#8482;',
349         '&#154;' => '&#353;',
350         '&#155;' => '&#8250;',
351         '&#156;' => '&#339;',
352         '&#157;' => '',
353         '&#158;' => '',
354         '&#159;' => '&#376;'
355         );
356
357         // Remove metadata tags
358         $content = preg_replace('/<title>(.+?)<\/title>/','',$content);
359         $content = preg_replace('/<category>(.+?)<\/category>/','',$content);
360
361         // Converts lone & characters into &#38; (a.k.a. &amp;)
362         $content = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/i', '&#038;$1', $content);
363
364         // Fix Word pasting
365         $content = strtr($content, $wp_htmltranswinuni);
366
367         // Just a little XHTML help
368         $content = str_replace('<br>', '<br />', $content);
369         $content = str_replace('<hr>', '<hr />', $content);
370
371         return $content;
372 }
373
374 function funky_javascript_fix($text) {
375         // Fixes for browsers' javascript bugs
376         global $is_macIE, $is_winIE;
377         
378         if ( $is_winIE || $is_macIE )
379                 $text =  preg_replace("/\%u([0-9A-F]{4,4})/e",  "'&#'.base_convert('\\1',16,10).';'", $text);
380         
381         return $text;
382 }
383
384 /*
385  balanceTags
386  
387  Balances Tags of string using a modified stack.
388  
389  @param text      Text to be balanced
390  @return          Returns balanced text
391  @author          Leonard Lin (leonard@acm.org)
392  @version         v1.1
393  @date            November 4, 2001
394  @license         GPL v2.0
395  @notes           
396  @changelog       
397  ---  Modified by Scott Reilly (coffee2code) 02 Aug 2004
398              1.2  ***TODO*** Make better - change loop condition to $text
399              1.1  Fixed handling of append/stack pop order of end text
400                   Added Cleaning Hooks
401              1.0  First Version
402 */
403 function balanceTags($text, $is_comment = 0) {
404         
405         if ( get_option('use_balanceTags') == 0)
406                 return $text;
407
408         $tagstack = array(); $stacksize = 0; $tagqueue = ''; $newtext = '';
409
410         # WP bug fix for comments - in case you REALLY meant to type '< !--'
411         $text = str_replace('< !--', '<    !--', $text);
412         # WP bug fix for LOVE <3 (and other situations with '<' before a number)
413         $text = preg_replace('#<([0-9]{1})#', '&lt;$1', $text);
414
415         while (preg_match("/<(\/?\w*)\s*([^>]*)>/",$text,$regex)) {
416                 $newtext .= $tagqueue;
417
418                 $i = strpos($text,$regex[0]);
419                 $l = strlen($regex[0]);
420
421                 // clear the shifter
422                 $tagqueue = '';
423                 // Pop or Push
424                 if ($regex[1][0] == "/") { // End Tag
425                         $tag = strtolower(substr($regex[1],1));
426                         // if too many closing tags
427                         if($stacksize <= 0) { 
428                                 $tag = '';
429                                 //or close to be safe $tag = '/' . $tag;
430                         }
431                         // if stacktop value = tag close value then pop
432                         else if ($tagstack[$stacksize - 1] == $tag) { // found closing tag
433                                 $tag = '</' . $tag . '>'; // Close Tag
434                                 // Pop
435                                 array_pop ($tagstack);
436                                 $stacksize--;
437                         } else { // closing tag not at top, search for it
438                                 for ($j=$stacksize-1;$j>=0;$j--) {
439                                         if ($tagstack[$j] == $tag) {
440                                         // add tag to tagqueue
441                                                 for ($k=$stacksize-1;$k>=$j;$k--){
442                                                         $tagqueue .= '</' . array_pop ($tagstack) . '>';
443                                                         $stacksize--;
444                                                 }
445                                                 break;
446                                         }
447                                 }
448                                 $tag = '';
449                         }
450                 } else { // Begin Tag
451                         $tag = strtolower($regex[1]);
452
453                         // Tag Cleaning
454
455                         // If self-closing or '', don't do anything.
456                         if((substr($regex[2],-1) == '/') || ($tag == '')) {
457                         }
458                         // ElseIf it's a known single-entity tag but it doesn't close itself, do so
459                         elseif ($tag == 'br' || $tag == 'img' || $tag == 'hr' || $tag == 'input') {
460                                 $regex[2] .= '/';
461                         } else {        // Push the tag onto the stack
462                                 // If the top of the stack is the same as the tag we want to push, close previous tag
463                                 if (($stacksize > 0) && ($tag != 'div') && ($tagstack[$stacksize - 1] == $tag)) {
464                                         $tagqueue = '</' . array_pop ($tagstack) . '>';
465                                         $stacksize--;
466                                 }
467                                 $stacksize = array_push ($tagstack, $tag);
468                         }
469
470                         // Attributes
471                         $attributes = $regex[2];
472                         if($attributes) {
473                                 $attributes = ' '.$attributes;
474                         }
475                         $tag = '<'.$tag.$attributes.'>';
476                         //If already queuing a close tag, then put this tag on, too
477                         if ($tagqueue) {
478                                 $tagqueue .= $tag;
479                                 $tag = '';
480                         }
481                 }
482                 $newtext .= substr($text,0,$i) . $tag;
483                 $text = substr($text,$i+$l);
484         }  
485
486         // Clear Tag Queue
487         $newtext .= $tagqueue;
488
489         // Add Remaining text
490         $newtext .= $text;
491
492         // Empty Stack
493         while($x = array_pop($tagstack)) {
494                 $newtext .= '</' . $x . '>'; // Add remaining tags to close
495         }
496
497         // WP fix for the bug with HTML comments
498         $newtext = str_replace("< !--","<!--",$newtext);
499         $newtext = str_replace("<    !--","< !--",$newtext);
500
501         return $newtext;
502 }
503
504
505 function format_to_edit($content, $richedit = false) {
506         $content = apply_filters('format_to_edit', $content);
507         if (! $richedit )
508                 $content = htmlspecialchars($content);
509         return $content;
510 }
511
512 function format_to_post($content) {
513         global $wpdb;
514         $content = apply_filters('format_to_post', $content);
515         return $content;
516 }
517
518 function zeroise($number,$threshold) { // function to add leading zeros when necessary
519         return sprintf('%0'.$threshold.'s', $number);
520         }
521
522
523 function backslashit($string) {
524         $string = preg_replace('/([a-z])/i', '\\\\\1', $string);
525         return $string;
526 }
527
528 function trailingslashit($string) {
529     if ( '/' != substr($string, -1)) {
530         $string .= '/';
531     }
532     return $string;
533 }
534
535 function addslashes_gpc($gpc) {
536         global $wpdb;
537
538         if (get_magic_quotes_gpc()) {
539                 $gpc = stripslashes($gpc);
540         }
541
542         return $wpdb->escape($gpc);
543 }
544
545
546 function stripslashes_deep($value)
547 {
548    $value = is_array($value) ?
549                array_map('stripslashes_deep', $value) :
550                stripslashes($value);
551
552    return $value;
553 }
554
555 function antispambot($emailaddy, $mailto=0) {
556         $emailNOSPAMaddy = '';
557         srand ((float) microtime() * 1000000);
558         for ($i = 0; $i < strlen($emailaddy); $i = $i + 1) {
559                 $j = floor(rand(0, 1+$mailto));
560                 if ($j==0) {
561                         $emailNOSPAMaddy .= '&#'.ord(substr($emailaddy,$i,1)).';';
562                 } elseif ($j==1) {
563                         $emailNOSPAMaddy .= substr($emailaddy,$i,1);
564                 } elseif ($j==2) {
565                         $emailNOSPAMaddy .= '%'.zeroise(dechex(ord(substr($emailaddy, $i, 1))), 2);
566                 }
567         }
568         $emailNOSPAMaddy = str_replace('@','&#64;',$emailNOSPAMaddy);
569         return $emailNOSPAMaddy;
570 }
571
572 function make_clickable($ret) {
573         $ret = ' ' . $ret . ' ';
574         $ret = preg_replace("#([\s>])(https?)://([^\s<>{}()]+[^\s.,<>{}()])#i", "$1<a href='$2://$3' rel='nofollow'>$2://$3</a>", $ret);
575         $ret = preg_replace("#(\s)www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^ <>{}()\n\r]*[^., <>{}()\n\r]?)?)#i", "$1<a href='http://www.$2.$3$4' rel='nofollow'>www.$2.$3$4</a>", $ret);
576         $ret = preg_replace("#(\s)([a-z0-9\-_.]+)@([a-z0-9\-_.]+)\.([^,< \n\r]+)#i", "$1<a href=\"mailto:$2@$3.$4\">$2@$3.$4</a>", $ret);
577         $ret = trim($ret);
578         return $ret;
579 }
580
581 function wp_rel_nofollow( $text ) {
582         $text = preg_replace('|<a (.+?)>|i', '<a $1 rel="nofollow">', $text);
583         return $text;
584 }
585
586 function convert_smilies($text) {
587         global $wp_smiliessearch, $wp_smiliesreplace;
588     $output = '';
589         if (get_settings('use_smilies')) {
590                 // HTML loop taken from texturize function, could possible be consolidated
591                 $textarr = preg_split("/(<.*>)/U", $text, -1, PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between
592                 $stop = count($textarr);// loop stuff
593                 for ($i = 0; $i < $stop; $i++) {
594                         $content = $textarr[$i];
595                         if ((strlen($content) > 0) && ('<' != $content{0})) { // If it's not a tag
596                                 $content = str_replace($wp_smiliessearch, $wp_smiliesreplace, $content);
597                         }
598                         $output .= $content;
599                 }
600         } else {
601                 // return default text.
602                 $output = $text;
603         }
604         return $output;
605 }
606
607
608 function is_email($user_email) {
609         $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
610         if(strstr($user_email, '@') && strstr($user_email, '.')) {
611                 if (preg_match($chars, $user_email)) {
612                         return true;
613                 } else {
614                         return false;
615                 }
616         } else {
617                 return false;
618         }
619 }
620
621 // used by wp-mail to handle charsets in email subjects
622 function wp_iso_descrambler($string) {
623   /* this may only work with iso-8859-1, I'm afraid */
624   if (!preg_match('#\=\?(.+)\?Q\?(.+)\?\=#i', $string, $matches)) {
625     return $string;
626   } else {
627     $subject = str_replace('_', ' ', $matches[2]);
628     $subject = preg_replace('#\=([0-9a-f]{2})#ei', "chr(hexdec(strtolower('$1')))", $subject);
629     return $subject;
630   }
631 }
632
633
634 // give it a date, it will give you the same date as GMT
635 function get_gmt_from_date($string) {
636   // note: this only substracts $time_difference from the given date
637   preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches);
638   $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
639   $string_gmt = gmdate('Y-m-d H:i:s', $string_time - get_settings('gmt_offset') * 3600);
640   return $string_gmt;
641 }
642
643 // give it a GMT date, it will give you the same date with $time_difference added
644 function get_date_from_gmt($string) {
645   // note: this only adds $time_difference to the given date
646   preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches);
647   $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
648   $string_localtime = gmdate('Y-m-d H:i:s', $string_time + get_settings('gmt_offset')*3600);
649   return $string_localtime;
650 }
651
652 // computes an offset in seconds from an iso8601 timezone
653 function iso8601_timezone_to_offset($timezone) {
654   // $timezone is either 'Z' or '[+|-]hhmm'
655   if ($timezone == 'Z') {
656     $offset = 0;
657   } else {
658     $sign    = (substr($timezone, 0, 1) == '+') ? 1 : -1;
659     $hours   = intval(substr($timezone, 1, 2));
660     $minutes = intval(substr($timezone, 3, 4)) / 60;
661     $offset  = $sign * 3600 * ($hours + $minutes);
662   }
663   return $offset;
664 }
665
666 // converts an iso8601 date to MySQL DateTime format used by post_date[_gmt]
667 function iso8601_to_datetime($date_string, $timezone = USER) {
668   if ($timezone == GMT) {
669     preg_match('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', $date_string, $date_bits);
670     if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset
671       $offset = iso8601_timezone_to_offset($date_bits[7]);
672     } else { // we don't have a timezone, so we assume user local timezone (not server's!)
673       $offset = 3600 * get_settings('gmt_offset');
674     }
675     $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
676     $timestamp -= $offset;
677     return gmdate('Y-m-d H:i:s', $timestamp);
678   } elseif ($timezone == USER) {
679     return preg_replace('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', '$1-$2-$3 $4:$5:$6', $date_string);
680   }
681 }
682
683 function popuplinks($text) {
684         // Comment text in popup windows should be filtered through this.
685         // Right now it's a moderately dumb function, ideally it would detect whether
686         // a target or rel attribute was already there and adjust its actions accordingly.
687         $text = preg_replace('/<a (.+?)>/i', "<a $1 target='_blank' rel='external'>", $text);
688         return $text;
689 }
690
691 function sanitize_email($email) {
692         return preg_replace('/[^a-z0-9+_.@-]/i', '', $email);
693 }
694
695 function human_time_diff( $from, $to = '' ) {     
696         if ( empty($to) )
697                 $to = time();
698         $diff = (int) abs($to - $from);
699         if ($diff <= 3600) {
700                 $mins = round($diff / 60);
701                 if ($mins <= 1)
702                         $since = __('1 min');
703                 else
704                         $since = sprintf( __('%s mins'), $mins);
705         } else if (($diff <= 86400) && ($diff > 3600)) {
706                 $hours = round($diff / 3600);
707                 if ($hours <= 1)
708                         $since = __('1 hour');
709                 else 
710                         $since = sprintf( __('%s hours'), $hours );
711         } elseif ($diff >= 86400) {
712                 $days = round($diff / 86400);
713                 if ($days <= 1)
714                         $since = __('1 day');
715                 else
716                         $since = sprintf( __('%s days'), $days );
717         }
718         return $since;
719 }
720
721 function wp_trim_excerpt($text) { // Fakes an excerpt if needed
722         global $post;
723         if ( '' == $text ) {
724                 $text = $post->post_content;
725                 $text = apply_filters('the_content', $text);
726                 $text = str_replace(']]>', ']]&gt;', $text);
727                 $text = strip_tags($text);
728                 $excerpt_length = 55;
729                 $words = explode(' ', $text, $excerpt_length + 1);
730                 if (count($words) > $excerpt_length) {
731                         array_pop($words);
732                         array_push($words, '[...]');
733                         $text = implode(' ', $words);
734                 }
735         }
736         return $text;
737 }
738
739 function ent2ncr($text) {
740         $to_ncr = array(
741                 '&quot;' => '&#34;',
742                 '&amp;' => '&#38;',
743                 '&frasl;' => '&#47;',
744                 '&lt;' => '&#60;',
745                 '&gt;' => '&#62;',
746                 '|' => '&#124;',
747                 '&nbsp;' => '&#160;',
748                 '&iexcl;' => '&#161;',
749                 '&cent;' => '&#162;',
750                 '&pound;' => '&#163;',
751                 '&curren;' => '&#164;',
752                 '&yen;' => '&#165;',
753                 '&brvbar;' => '&#166;',
754                 '&brkbar;' => '&#166;',
755                 '&sect;' => '&#167;',
756                 '&uml;' => '&#168;',
757                 '&die;' => '&#168;',
758                 '&copy;' => '&#169;',
759                 '&ordf;' => '&#170;',
760                 '&laquo;' => '&#171;',
761                 '&not;' => '&#172;',
762                 '&shy;' => '&#173;',
763                 '&reg;' => '&#174;',
764                 '&macr;' => '&#175;',
765                 '&hibar;' => '&#175;',
766                 '&deg;' => '&#176;',
767                 '&plusmn;' => '&#177;',
768                 '&sup2;' => '&#178;',
769                 '&sup3;' => '&#179;',
770                 '&acute;' => '&#180;',
771                 '&micro;' => '&#181;',
772                 '&para;' => '&#182;',
773                 '&middot;' => '&#183;',
774                 '&cedil;' => '&#184;',
775                 '&sup1;' => '&#185;',
776                 '&ordm;' => '&#186;',
777                 '&raquo;' => '&#187;',
778                 '&frac14;' => '&#188;',
779                 '&frac12;' => '&#189;',
780                 '&frac34;' => '&#190;',
781                 '&iquest;' => '&#191;',
782                 '&Agrave;' => '&#192;',
783                 '&Aacute;' => '&#193;',
784                 '&Acirc;' => '&#194;',
785                 '&Atilde;' => '&#195;',
786                 '&Auml;' => '&#196;',
787                 '&Aring;' => '&#197;',
788                 '&AElig;' => '&#198;',
789                 '&Ccedil;' => '&#199;',
790                 '&Egrave;' => '&#200;',
791                 '&Eacute;' => '&#201;',
792                 '&Ecirc;' => '&#202;',
793                 '&Euml;' => '&#203;',
794                 '&Igrave;' => '&#204;',
795                 '&Iacute;' => '&#205;',
796                 '&Icirc;' => '&#206;',
797                 '&Iuml;' => '&#207;',
798                 '&ETH;' => '&#208;',
799                 '&Ntilde;' => '&#209;',
800                 '&Ograve;' => '&#210;',
801                 '&Oacute;' => '&#211;',
802                 '&Ocirc;' => '&#212;',
803                 '&Otilde;' => '&#213;',
804                 '&Ouml;' => '&#214;',
805                 '&times;' => '&#215;',
806                 '&Oslash;' => '&#216;',
807                 '&Ugrave;' => '&#217;',
808                 '&Uacute;' => '&#218;',
809                 '&Ucirc;' => '&#219;',
810                 '&Uuml;' => '&#220;',
811                 '&Yacute;' => '&#221;',
812                 '&THORN;' => '&#222;',
813                 '&szlig;' => '&#223;',
814                 '&agrave;' => '&#224;',
815                 '&aacute;' => '&#225;',
816                 '&acirc;' => '&#226;',
817                 '&atilde;' => '&#227;',
818                 '&auml;' => '&#228;',
819                 '&aring;' => '&#229;',
820                 '&aelig;' => '&#230;',
821                 '&ccedil;' => '&#231;',
822                 '&egrave;' => '&#232;',
823                 '&eacute;' => '&#233;',
824                 '&ecirc;' => '&#234;',
825                 '&euml;' => '&#235;',
826                 '&igrave;' => '&#236;',
827                 '&iacute;' => '&#237;',
828                 '&icirc;' => '&#238;',
829                 '&iuml;' => '&#239;',
830                 '&eth;' => '&#240;',
831                 '&ntilde;' => '&#241;',
832                 '&ograve;' => '&#242;',
833                 '&oacute;' => '&#243;',
834                 '&ocirc;' => '&#244;',
835                 '&otilde;' => '&#245;',
836                 '&ouml;' => '&#246;',
837                 '&divide;' => '&#247;',
838                 '&oslash;' => '&#248;',
839                 '&ugrave;' => '&#249;',
840                 '&uacute;' => '&#250;',
841                 '&ucirc;' => '&#251;',
842                 '&uuml;' => '&#252;',
843                 '&yacute;' => '&#253;',
844                 '&thorn;' => '&#254;',
845                 '&yuml;' => '&#255;',
846                 '&OElig;' => '&#338;',
847                 '&oelig;' => '&#339;',
848                 '&Scaron;' => '&#352;',
849                 '&scaron;' => '&#353;',
850                 '&Yuml;' => '&#376;',
851                 '&fnof;' => '&#402;',
852                 '&circ;' => '&#710;',
853                 '&tilde;' => '&#732;',
854                 '&Alpha;' => '&#913;',
855                 '&Beta;' => '&#914;',
856                 '&Gamma;' => '&#915;',
857                 '&Delta;' => '&#916;',
858                 '&Epsilon;' => '&#917;',
859                 '&Zeta;' => '&#918;',
860                 '&Eta;' => '&#919;',
861                 '&Theta;' => '&#920;',
862                 '&Iota;' => '&#921;',
863                 '&Kappa;' => '&#922;',
864                 '&Lambda;' => '&#923;',
865                 '&Mu;' => '&#924;',
866                 '&Nu;' => '&#925;',
867                 '&Xi;' => '&#926;',
868                 '&Omicron;' => '&#927;',
869                 '&Pi;' => '&#928;',
870                 '&Rho;' => '&#929;',
871                 '&Sigma;' => '&#931;',
872                 '&Tau;' => '&#932;',
873                 '&Upsilon;' => '&#933;',
874                 '&Phi;' => '&#934;',
875                 '&Chi;' => '&#935;',
876                 '&Psi;' => '&#936;',
877                 '&Omega;' => '&#937;',
878                 '&alpha;' => '&#945;',
879                 '&beta;' => '&#946;',
880                 '&gamma;' => '&#947;',
881                 '&delta;' => '&#948;',
882                 '&epsilon;' => '&#949;',
883                 '&zeta;' => '&#950;',
884                 '&eta;' => '&#951;',
885                 '&theta;' => '&#952;',
886                 '&iota;' => '&#953;',
887                 '&kappa;' => '&#954;',
888                 '&lambda;' => '&#955;',
889                 '&mu;' => '&#956;',
890                 '&nu;' => '&#957;',
891                 '&xi;' => '&#958;',
892                 '&omicron;' => '&#959;',
893                 '&pi;' => '&#960;',
894                 '&rho;' => '&#961;',
895                 '&sigmaf;' => '&#962;',
896                 '&sigma;' => '&#963;',
897                 '&tau;' => '&#964;',
898                 '&upsilon;' => '&#965;',
899                 '&phi;' => '&#966;',
900                 '&chi;' => '&#967;',
901                 '&psi;' => '&#968;',
902                 '&omega;' => '&#969;',
903                 '&thetasym;' => '&#977;',
904                 '&upsih;' => '&#978;',
905                 '&piv;' => '&#982;',
906                 '&ensp;' => '&#8194;',
907                 '&emsp;' => '&#8195;',
908                 '&thinsp;' => '&#8201;',
909                 '&zwnj;' => '&#8204;',
910                 '&zwj;' => '&#8205;',
911                 '&lrm;' => '&#8206;',
912                 '&rlm;' => '&#8207;',
913                 '&ndash;' => '&#8211;',
914                 '&mdash;' => '&#8212;',
915                 '&lsquo;' => '&#8216;',
916                 '&rsquo;' => '&#8217;',
917                 '&sbquo;' => '&#8218;',
918                 '&ldquo;' => '&#8220;',
919                 '&rdquo;' => '&#8221;',
920                 '&bdquo;' => '&#8222;',
921                 '&dagger;' => '&#8224;',
922                 '&Dagger;' => '&#8225;',
923                 '&bull;' => '&#8226;',
924                 '&hellip;' => '&#8230;',
925                 '&permil;' => '&#8240;',
926                 '&prime;' => '&#8242;',
927                 '&Prime;' => '&#8243;',
928                 '&lsaquo;' => '&#8249;',
929                 '&rsaquo;' => '&#8250;',
930                 '&oline;' => '&#8254;',
931                 '&frasl;' => '&#8260;',
932                 '&euro;' => '&#8364;',
933                 '&image;' => '&#8465;',
934                 '&weierp;' => '&#8472;',
935                 '&real;' => '&#8476;',
936                 '&trade;' => '&#8482;',
937                 '&alefsym;' => '&#8501;',
938                 '&crarr;' => '&#8629;',
939                 '&lArr;' => '&#8656;',
940                 '&uArr;' => '&#8657;',
941                 '&rArr;' => '&#8658;',
942                 '&dArr;' => '&#8659;',
943                 '&hArr;' => '&#8660;',
944                 '&forall;' => '&#8704;',
945                 '&part;' => '&#8706;',
946                 '&exist;' => '&#8707;',
947                 '&empty;' => '&#8709;',
948                 '&nabla;' => '&#8711;',
949                 '&isin;' => '&#8712;',
950                 '&notin;' => '&#8713;',
951                 '&ni;' => '&#8715;',
952                 '&prod;' => '&#8719;',
953                 '&sum;' => '&#8721;',
954                 '&minus;' => '&#8722;',
955                 '&lowast;' => '&#8727;',
956                 '&radic;' => '&#8730;',
957                 '&prop;' => '&#8733;',
958                 '&infin;' => '&#8734;',
959                 '&ang;' => '&#8736;',
960                 '&and;' => '&#8743;',
961                 '&or;' => '&#8744;',
962                 '&cap;' => '&#8745;',
963                 '&cup;' => '&#8746;',
964                 '&int;' => '&#8747;',
965                 '&there4;' => '&#8756;',
966                 '&sim;' => '&#8764;',
967                 '&cong;' => '&#8773;',
968                 '&asymp;' => '&#8776;',
969                 '&ne;' => '&#8800;',
970                 '&equiv;' => '&#8801;',
971                 '&le;' => '&#8804;',
972                 '&ge;' => '&#8805;',
973                 '&sub;' => '&#8834;',
974                 '&sup;' => '&#8835;',
975                 '&nsub;' => '&#8836;',
976                 '&sube;' => '&#8838;',
977                 '&supe;' => '&#8839;',
978                 '&oplus;' => '&#8853;',
979                 '&otimes;' => '&#8855;',
980                 '&perp;' => '&#8869;',
981                 '&sdot;' => '&#8901;',
982                 '&lceil;' => '&#8968;',
983                 '&rceil;' => '&#8969;',
984                 '&lfloor;' => '&#8970;',
985                 '&rfloor;' => '&#8971;',
986                 '&lang;' => '&#9001;',
987                 '&rang;' => '&#9002;',
988                 '&larr;' => '&#8592;',
989                 '&uarr;' => '&#8593;',
990                 '&rarr;' => '&#8594;',
991                 '&darr;' => '&#8595;',
992                 '&harr;' => '&#8596;',
993                 '&loz;' => '&#9674;',
994                 '&spades;' => '&#9824;',
995                 '&clubs;' => '&#9827;',
996                 '&hearts;' => '&#9829;',
997                 '&diams;' => '&#9830;'
998         );
999
1000         foreach ($to_ncr as $entity => $ncr) {
1001                 $text = str_replace($entity, $ncr, $text);
1002         }
1003         return $text;
1004 }
1005
1006 function wp_richedit_pre($text) {
1007         // Filtering a blank results in an annoying <br />\n
1008         if ( empty($text) ) return apply_filters('richedit_pre', '');
1009
1010         $output = $text;
1011         $output = convert_chars($output);
1012         $output = wpautop($output);
1013
1014         // These must be double-escaped or planets will collide.
1015         $output = str_replace('&lt;', '&amp;lt;', $output);
1016         $output = str_replace('&gt;', '&amp;gt;', $output);
1017
1018         return apply_filters('richedit_pre', $output);
1019 }
1020
1021 ?>