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