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