3 function wptexturize($text) {
4 global $wp_cockneyreplace;
8 $textarr = preg_split('/(<.*>)/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
9 $stop = count($textarr);
11 // if a plugin has provided an autocorrect array, use it
12 if ( isset($wp_cockneyreplace) ) {
13 $cockney = array_keys($wp_cockneyreplace);
14 $cockneyreplace = array_values($wp_cockneyreplace);
16 $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");
17 $cockneyreplace = array("’tain’t","’twere","’twas","’tis","’twill","’til","’bout","’nuff","’round","’cause");
20 $static_characters = array_merge(array('---', ' -- ', '--', 'xn–', '...', '``', '\'s', '\'\'', ' (tm)'), $cockney);
21 $static_replacements = array_merge(array('—', ' — ', '–', 'xn--', '…', '“', '’s', '”', ' ™'), $cockneyreplace);
23 $dynamic_characters = array('/\'(\d\d(?:’|\')?s)/', '/(\s|\A|")\'/', '/(\d+)"/', '/(\d+)\'/', '/(\S)\'([^\'\s])/', '/(\s|\A)"(?!\s)/', '/"(\s|\S|\Z)/', '/\'([\s.]|\Z)/', '/(\d+)x(\d+)/');
24 $dynamic_replacements = array('’$1','$1‘', '$1″', '$1′', '$1’$2', '$1“$2', '”$1', '’$1', '$1×$2');
26 for ( $i = 0; $i < $stop; $i++ ) {
29 if (isset($curl{0}) && '<' != $curl{0} && $next) { // If it's not a tag
31 $curl = str_replace($static_characters, $static_replacements, $curl);
32 // regular expressions
33 $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
34 } elseif (strpos($curl, '<code') !== false || strpos($curl, '<pre') !== false || strpos($curl, '<kbd') !== false || strpos($curl, '<style') !== false || strpos($curl, '<script') !== false) {
40 $curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&$1', $curl);
47 // Accepts matches array from preg_replace_callback in wpautop()
49 function clean_pre($matches) {
50 if ( is_array($matches) )
51 $text = $matches[1] . $matches[2] . "</pre>";
55 $text = str_replace('<br />', '', $text);
56 $text = str_replace('<p>', "\n", $text);
57 $text = str_replace('</p>', '', $text);
62 function wpautop($pee, $br = 1) {
63 $pee = $pee . "\n"; // just to make things a little easier, pad the end
64 $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
65 // Space things out a little
66 $allblocks = '(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr)';
67 $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
68 $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
69 $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
70 $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
71 $pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $pee); // make paragraphs, including one at the end
72 $pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
73 $pee = preg_replace('!<p>([^<]+)\s*?(</(?:div|address|form)[^>]*>)!', "<p>$1</p>$2", $pee);
74 $pee = preg_replace( '|<p>|', "$1<p>", $pee );
75 $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
76 $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
77 $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
78 $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
79 $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
80 $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
82 $pee = preg_replace('/<(script|style).*?<\/\\1>/se', 'str_replace("\n", "<WPPreserveNewline />", "\\0")', $pee);
83 $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
84 $pee = str_replace('<WPPreserveNewline />', "\n", $pee);
86 $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
87 $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
88 if (strpos($pee, '<pre') !== false)
89 $pee = preg_replace_callback('!(<pre.*?>)(.*?)</pre>!is', 'clean_pre', $pee );
90 $pee = preg_replace( "|\n</p>$|", '</p>', $pee );
96 function seems_utf8($Str) { # by bmorel at ssi dot fr
97 for ($i=0; $i<strlen($Str); $i++) {
98 if (ord($Str[$i]) < 0x80) continue; # 0bbbbbbb
99 elseif ((ord($Str[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
100 elseif ((ord($Str[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
101 elseif ((ord($Str[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb
102 elseif ((ord($Str[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb
103 elseif ((ord($Str[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
104 else return false; # Does not match any model
105 for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
106 if ((++$i == strlen($Str)) || ((ord($Str[$i]) & 0xC0) != 0x80))
113 function wp_specialchars( $text, $quotes = 0 ) {
114 // Like htmlspecialchars except don't double-encode HTML entities
115 $text = str_replace('&&', '&&', $text);
116 $text = str_replace('&&', '&&', $text);
117 $text = preg_replace('/&(?:$|([^#])(?![a-z1-4]{1,8};))/', '&$1', $text);
118 $text = str_replace('<', '<', $text);
119 $text = str_replace('>', '>', $text);
120 if ( 'double' === $quotes ) {
121 $text = str_replace('"', '"', $text);
122 } elseif ( 'single' === $quotes ) {
123 $text = str_replace("'", ''', $text);
124 } elseif ( $quotes ) {
125 $text = str_replace('"', '"', $text);
126 $text = str_replace("'", ''', $text);
131 function utf8_uri_encode( $utf8_string, $length = 0 ) {
136 for ($i = 0; $i < strlen( $utf8_string ); $i++ ) {
138 $value = ord( $utf8_string[ $i ] );
140 if ( $value < 128 ) {
141 if ( $length && ( strlen($unicode) + 1 > $length ) )
143 $unicode .= chr($value);
145 if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3;
149 if ( $length && ( (strlen($unicode) + ($num_octets * 3)) > $length ) )
151 if ( count( $values ) == $num_octets ) {
152 if ($num_octets == 3) {
153 $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]);
155 $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]);
167 function remove_accents($string) {
168 if ( !preg_match('/[\x80-\xff]/', $string) )
171 if (seems_utf8($string)) {
173 // Decompositions for Latin-1 Supplement
174 chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
175 chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
176 chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
177 chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
178 chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
179 chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
180 chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
181 chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
182 chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
183 chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
184 chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
185 chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
186 chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
187 chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
188 chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
189 chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
190 chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
191 chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
192 chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
193 chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
194 chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
195 chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
196 chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
197 chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
198 chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
199 chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
200 chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
201 chr(195).chr(191) => 'y',
202 // Decompositions for Latin Extended-A
203 chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
204 chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
205 chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
206 chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
207 chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
208 chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
209 chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
210 chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
211 chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
212 chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
213 chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
214 chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
215 chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
216 chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
217 chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
218 chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
219 chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
220 chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
221 chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
222 chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
223 chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
224 chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
225 chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
226 chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
227 chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
228 chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
229 chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
230 chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
231 chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
232 chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
233 chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
234 chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
235 chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
236 chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
237 chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
238 chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
239 chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
240 chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
241 chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
242 chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
243 chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
244 chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
245 chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
246 chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
247 chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
248 chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
249 chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
250 chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
251 chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
252 chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
253 chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
254 chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
255 chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
256 chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
257 chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
258 chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
259 chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
260 chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
261 chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
262 chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
263 chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
264 chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
265 chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
266 chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
268 chr(226).chr(130).chr(172) => 'E',
270 chr(194).chr(163) => '');
272 $string = strtr($string, $chars);
274 // Assume ISO-8859-1 if not UTF-8
275 $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
276 .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
277 .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
278 .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
279 .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
280 .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
281 .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
282 .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
283 .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
284 .chr(252).chr(253).chr(255);
286 $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
288 $string = strtr($string, $chars['in'], $chars['out']);
289 $double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
290 $double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
291 $string = str_replace($double_chars['in'], $double_chars['out'], $string);
297 function sanitize_file_name( $name ) { // Like sanitize_title, but with periods
298 $name = strtolower( $name );
299 $name = preg_replace('/&.+?;/', '', $name); // kill entities
300 $name = str_replace( '_', '-', $name );
301 $name = preg_replace('/[^a-z0-9\s-.]/', '', $name);
302 $name = preg_replace('/\s+/', '-', $name);
303 $name = preg_replace('|-+|', '-', $name);
304 $name = trim($name, '-');
308 function sanitize_user( $username, $strict = false ) {
309 $raw_username = $username;
310 $username = strip_tags($username);
312 $username = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '', $username);
313 $username = preg_replace('/&.+?;/', '', $username); // Kill entities
315 // If strict, reduce to ASCII for max portability.
317 $username = preg_replace('|[^a-z0-9 _.\-@]|i', '', $username);
319 return apply_filters('sanitize_user', $username, $raw_username, $strict);
322 function sanitize_title($title, $fallback_title = '') {
323 $title = strip_tags($title);
324 $title = apply_filters('sanitize_title', $title);
327 $title = $fallback_title;
333 function sanitize_title_with_dashes($title) {
334 $title = strip_tags($title);
335 // Preserve escaped octets.
336 $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
337 // Remove percent signs that are not part of an octet.
338 $title = str_replace('%', '', $title);
340 $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
342 $title = remove_accents($title);
343 if (seems_utf8($title)) {
344 if (function_exists('mb_strtolower')) {
345 $title = mb_strtolower($title, 'UTF-8');
347 $title = utf8_uri_encode($title, 200);
350 $title = strtolower($title);
351 $title = preg_replace('/&.+?;/', '', $title); // kill entities
352 $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
353 $title = preg_replace('/\s+/', '-', $title);
354 $title = preg_replace('|-+|', '-', $title);
355 $title = trim($title, '-');
360 function convert_chars($content, $flag = 'obsolete') {
361 // Translation of invalid Unicode references range to valid range
362 $wp_htmltranswinuni = array(
363 '€' => '€', // the Euro sign
365 '‚' => '‚', // these are Windows CP1252 specific characters
366 'ƒ' => 'ƒ', // they would look weird on non-Windows browsers
367 '„' => '„',
368 '…' => '…',
369 '†' => '†',
370 '‡' => '‡',
371 'ˆ' => 'ˆ',
372 '‰' => '‰',
373 'Š' => 'Š',
374 '‹' => '‹',
375 'Œ' => 'Œ',
377 'Ž' => 'ž',
380 '‘' => '‘',
381 '’' => '’',
382 '“' => '“',
383 '”' => '”',
384 '•' => '•',
385 '–' => '–',
386 '—' => '—',
387 '˜' => '˜',
388 '™' => '™',
389 'š' => 'š',
390 '›' => '›',
391 'œ' => 'œ',
397 // Remove metadata tags
398 $content = preg_replace('/<title>(.+?)<\/title>/','',$content);
399 $content = preg_replace('/<category>(.+?)<\/category>/','',$content);
401 // Converts lone & characters into & (a.k.a. &)
402 $content = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/i', '&$1', $content);
405 $content = strtr($content, $wp_htmltranswinuni);
407 // Just a little XHTML help
408 $content = str_replace('<br>', '<br />', $content);
409 $content = str_replace('<hr>', '<hr />', $content);
414 function funky_javascript_fix($text) {
415 // Fixes for browsers' javascript bugs
416 global $is_macIE, $is_winIE;
418 if ( $is_winIE || $is_macIE )
419 $text = preg_replace("/\%u([0-9A-F]{4,4})/e", "'&#'.base_convert('\\1',16,10).';'", $text);
424 function balanceTags( $text, $force = false ) {
425 if ( !$force && get_option('use_balanceTags') == 0 )
427 return force_balance_tags( $text );
433 Balances Tags of string using a modified stack.
435 @param text Text to be balanced
436 @param force Forces balancing, ignoring the value of the option
437 @return Returns balanced text
438 @author Leonard Lin (leonard@acm.org)
440 @date November 4, 2001
444 --- Modified by Scott Reilly (coffee2code) 02 Aug 2004
445 1.2 ***TODO*** Make better - change loop condition to $text
446 1.1 Fixed handling of append/stack pop order of end text
450 function force_balance_tags( $text ) {
451 $tagstack = array(); $stacksize = 0; $tagqueue = ''; $newtext = '';
452 $single_tags = array('br', 'hr', 'img', 'input'); //Known single-entity/self-closing tags
453 $nestable_tags = array('blockquote', 'div', 'span'); //Tags that can be immediately nested within themselves
455 # WP bug fix for comments - in case you REALLY meant to type '< !--'
456 $text = str_replace('< !--', '< !--', $text);
457 # WP bug fix for LOVE <3 (and other situations with '<' before a number)
458 $text = preg_replace('#<([0-9]{1})#', '<$1', $text);
460 while (preg_match("/<(\/?\w*)\s*([^>]*)>/",$text,$regex)) {
461 $newtext .= $tagqueue;
463 $i = strpos($text,$regex[0]);
464 $l = strlen($regex[0]);
469 if ($regex[1][0] == "/") { // End Tag
470 $tag = strtolower(substr($regex[1],1));
471 // if too many closing tags
472 if($stacksize <= 0) {
474 //or close to be safe $tag = '/' . $tag;
476 // if stacktop value = tag close value then pop
477 else if ($tagstack[$stacksize - 1] == $tag) { // found closing tag
478 $tag = '</' . $tag . '>'; // Close Tag
480 array_pop ($tagstack);
482 } else { // closing tag not at top, search for it
483 for ($j=$stacksize-1;$j>=0;$j--) {
484 if ($tagstack[$j] == $tag) {
485 // add tag to tagqueue
486 for ($k=$stacksize-1;$k>=$j;$k--){
487 $tagqueue .= '</' . array_pop ($tagstack) . '>';
495 } else { // Begin Tag
496 $tag = strtolower($regex[1]);
500 // If self-closing or '', don't do anything.
501 if((substr($regex[2],-1) == '/') || ($tag == '')) {
503 // ElseIf it's a known single-entity tag but it doesn't close itself, do so
504 elseif ( in_array($tag, $single_tags) ) {
506 } else { // Push the tag onto the stack
507 // If the top of the stack is the same as the tag we want to push, close previous tag
508 if (($stacksize > 0) && !in_array($tag, $nestable_tags) && ($tagstack[$stacksize - 1] == $tag)) {
509 $tagqueue = '</' . array_pop ($tagstack) . '>';
512 $stacksize = array_push ($tagstack, $tag);
516 $attributes = $regex[2];
518 $attributes = ' '.$attributes;
520 $tag = '<'.$tag.$attributes.'>';
521 //If already queuing a close tag, then put this tag on, too
527 $newtext .= substr($text,0,$i) . $tag;
528 $text = substr($text,$i+$l);
532 $newtext .= $tagqueue;
534 // Add Remaining text
538 while($x = array_pop($tagstack)) {
539 $newtext .= '</' . $x . '>'; // Add remaining tags to close
542 // WP fix for the bug with HTML comments
543 $newtext = str_replace("< !--","<!--",$newtext);
544 $newtext = str_replace("< !--","< !--",$newtext);
549 function format_to_edit($content, $richedit = false) {
550 $content = apply_filters('format_to_edit', $content);
552 $content = htmlspecialchars($content);
556 function format_to_post($content) {
558 $content = apply_filters('format_to_post', $content);
562 function zeroise($number,$threshold) { // function to add leading zeros when necessary
563 return sprintf('%0'.$threshold.'s', $number);
567 function backslashit($string) {
568 $string = preg_replace('/^([0-9])/', '\\\\\\\\\1', $string);
569 $string = preg_replace('/([a-z])/i', '\\\\\1', $string);
573 function trailingslashit($string) {
574 return untrailingslashit($string) . '/';
577 function untrailingslashit($string) {
578 return rtrim($string, '/');
581 function addslashes_gpc($gpc) {
584 if (get_magic_quotes_gpc()) {
585 $gpc = stripslashes($gpc);
588 return $wpdb->escape($gpc);
592 function stripslashes_deep($value) {
593 $value = is_array($value) ?
594 array_map('stripslashes_deep', $value) :
595 stripslashes($value);
600 function urlencode_deep($value) {
601 $value = is_array($value) ?
602 array_map('urlencode_deep', $value) :
608 function antispambot($emailaddy, $mailto=0) {
609 $emailNOSPAMaddy = '';
610 srand ((float) microtime() * 1000000);
611 for ($i = 0; $i < strlen($emailaddy); $i = $i + 1) {
612 $j = floor(rand(0, 1+$mailto));
614 $emailNOSPAMaddy .= '&#'.ord(substr($emailaddy,$i,1)).';';
616 $emailNOSPAMaddy .= substr($emailaddy,$i,1);
618 $emailNOSPAMaddy .= '%'.zeroise(dechex(ord(substr($emailaddy, $i, 1))), 2);
621 $emailNOSPAMaddy = str_replace('@','@',$emailNOSPAMaddy);
622 return $emailNOSPAMaddy;
625 function _make_url_clickable_cb($matches) {
627 $url = clean_url($url);
630 return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>";
633 function _make_web_ftp_clickable_cb($matches) {
635 $dest = 'http://' . $dest;
636 $dest = clean_url($dest);
640 return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>";
643 function _make_email_clickable_cb($matches) {
644 $email = $matches[2] . '@' . $matches[3];
645 return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
648 function make_clickable($ret) {
650 // in testing, using arrays here was found to be faster
651 $ret = preg_replace_callback('#([\s>])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_url_clickable_cb', $ret);
652 $ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_web_ftp_clickable_cb', $ret);
653 $ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret);
654 // this one is not in an array because we need it to run last, for cleanup of accidental links within links
655 $ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);
660 function wp_rel_nofollow( $text ) {
662 // This is a pre save filter, so text is already escaped.
663 $text = stripslashes($text);
664 $text = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $text);
665 $text = $wpdb->escape($text);
669 function wp_rel_nofollow_callback( $matches ) {
671 $text = str_replace(array(' rel="nofollow"', " rel='nofollow'"), '', $text);
672 return "<a $text rel=\"nofollow\">";
675 function convert_smilies($text) {
676 global $wp_smiliessearch, $wp_smiliesreplace;
678 if (get_option('use_smilies')) {
679 // HTML loop taken from texturize function, could possible be consolidated
680 $textarr = preg_split("/(<.*>)/U", $text, -1, PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between
681 $stop = count($textarr);// loop stuff
682 for ($i = 0; $i < $stop; $i++) {
683 $content = $textarr[$i];
684 if ((strlen($content) > 0) && ('<' != $content{0})) { // If it's not a tag
685 $content = preg_replace($wp_smiliessearch, $wp_smiliesreplace, $content);
690 // return default text.
697 function is_email($user_email) {
698 $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
699 if (strpos($user_email, '@') !== false && strpos($user_email, '.') !== false) {
700 if (preg_match($chars, $user_email)) {
710 // used by wp-mail to handle charsets in email subjects
711 function wp_iso_descrambler($string) {
712 /* this may only work with iso-8859-1, I'm afraid */
713 if (!preg_match('#\=\?(.+)\?Q\?(.+)\?\=#i', $string, $matches)) {
716 $subject = str_replace('_', ' ', $matches[2]);
717 $subject = preg_replace('#\=([0-9a-f]{2})#ei', "chr(hexdec(strtolower('$1')))", $subject);
723 // give it a date, it will give you the same date as GMT
724 function get_gmt_from_date($string) {
725 // note: this only substracts $time_difference from the given date
726 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);
727 $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
728 $string_gmt = gmdate('Y-m-d H:i:s', $string_time - get_option('gmt_offset') * 3600);
732 // give it a GMT date, it will give you the same date with $time_difference added
733 function get_date_from_gmt($string) {
734 // note: this only adds $time_difference to the given date
735 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);
736 $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
737 $string_localtime = gmdate('Y-m-d H:i:s', $string_time + get_option('gmt_offset')*3600);
738 return $string_localtime;
741 // computes an offset in seconds from an iso8601 timezone
742 function iso8601_timezone_to_offset($timezone) {
743 // $timezone is either 'Z' or '[+|-]hhmm'
744 if ($timezone == 'Z') {
747 $sign = (substr($timezone, 0, 1) == '+') ? 1 : -1;
748 $hours = intval(substr($timezone, 1, 2));
749 $minutes = intval(substr($timezone, 3, 4)) / 60;
750 $offset = $sign * 3600 * ($hours + $minutes);
755 // converts an iso8601 date to MySQL DateTime format used by post_date[_gmt]
756 function iso8601_to_datetime($date_string, $timezone = USER) {
757 if ($timezone == GMT) {
758 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);
759 if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset
760 $offset = iso8601_timezone_to_offset($date_bits[7]);
761 } else { // we don't have a timezone, so we assume user local timezone (not server's!)
762 $offset = 3600 * get_option('gmt_offset');
764 $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
765 $timestamp -= $offset;
766 return gmdate('Y-m-d H:i:s', $timestamp);
767 } elseif ($timezone == USER) {
768 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);
772 function popuplinks($text) {
773 // Comment text in popup windows should be filtered through this.
774 // Right now it's a moderately dumb function, ideally it would detect whether
775 // a target or rel attribute was already there and adjust its actions accordingly.
776 $text = preg_replace('/<a (.+?)>/i', "<a $1 target='_blank' rel='external'>", $text);
780 function sanitize_email($email) {
781 return preg_replace('/[^a-z0-9+_.@-]/i', '', $email);
784 function human_time_diff( $from, $to = '' ) {
787 $diff = (int) abs($to - $from);
789 $mins = round($diff / 60);
793 $since = sprintf(__ngettext('%s min', '%s mins', $mins), $mins);
794 } else if (($diff <= 86400) && ($diff > 3600)) {
795 $hours = round($diff / 3600);
799 $since = sprintf(__ngettext('%s hour', '%s hours', $hours), $hours);
800 } elseif ($diff >= 86400) {
801 $days = round($diff / 86400);
805 $since = sprintf(__ngettext('%s day', '%s days', $days), $days);
810 function wp_trim_excerpt($text) { // Fakes an excerpt if needed
813 $text = get_the_content('');
814 $text = apply_filters('the_content', $text);
815 $text = str_replace(']]>', ']]>', $text);
816 $text = strip_tags($text);
817 $excerpt_length = 55;
818 $words = explode(' ', $text, $excerpt_length + 1);
819 if (count($words) > $excerpt_length) {
821 array_push($words, '[...]');
822 $text = implode(' ', $words);
828 function ent2ncr($text) {
832 '⁄' => '/',
836 ' ' => ' ',
837 '¡' => '¡',
838 '¢' => '¢',
839 '£' => '£',
840 '¤' => '¤',
842 '¦' => '¦',
843 '&brkbar;' => '¦',
844 '§' => '§',
847 '©' => '©',
848 'ª' => 'ª',
849 '«' => '«',
853 '¯' => '¯',
854 '&hibar;' => '¯',
856 '±' => '±',
857 '²' => '²',
858 '³' => '³',
859 '´' => '´',
860 'µ' => 'µ',
861 '¶' => '¶',
862 '·' => '·',
863 '¸' => '¸',
864 '¹' => '¹',
865 'º' => 'º',
866 '»' => '»',
867 '¼' => '¼',
868 '½' => '½',
869 '¾' => '¾',
870 '¿' => '¿',
871 'À' => 'À',
872 'Á' => 'Á',
873 'Â' => 'Â',
874 'Ã' => 'Ã',
875 'Ä' => 'Ä',
876 'Å' => 'Å',
877 'Æ' => 'Æ',
878 'Ç' => 'Ç',
879 'È' => 'È',
880 'É' => 'É',
881 'Ê' => 'Ê',
882 'Ë' => 'Ë',
883 'Ì' => 'Ì',
884 'Í' => 'Í',
885 'Î' => 'Î',
886 'Ï' => 'Ï',
888 'Ñ' => 'Ñ',
889 'Ò' => 'Ò',
890 'Ó' => 'Ó',
891 'Ô' => 'Ô',
892 'Õ' => 'Õ',
893 'Ö' => 'Ö',
894 '×' => '×',
895 'Ø' => 'Ø',
896 'Ù' => 'Ù',
897 'Ú' => 'Ú',
898 'Û' => 'Û',
899 'Ü' => 'Ü',
900 'Ý' => 'Ý',
901 'Þ' => 'Þ',
902 'ß' => 'ß',
903 'à' => 'à',
904 'á' => 'á',
905 'â' => 'â',
906 'ã' => 'ã',
907 'ä' => 'ä',
908 'å' => 'å',
909 'æ' => 'æ',
910 'ç' => 'ç',
911 'è' => 'è',
912 'é' => 'é',
913 'ê' => 'ê',
914 'ë' => 'ë',
915 'ì' => 'ì',
916 'í' => 'í',
917 'î' => 'î',
918 'ï' => 'ï',
920 'ñ' => 'ñ',
921 'ò' => 'ò',
922 'ó' => 'ó',
923 'ô' => 'ô',
924 'õ' => 'õ',
925 'ö' => 'ö',
926 '÷' => '÷',
927 'ø' => 'ø',
928 'ù' => 'ù',
929 'ú' => 'ú',
930 'û' => 'û',
931 'ü' => 'ü',
932 'ý' => 'ý',
933 'þ' => 'þ',
934 'ÿ' => 'ÿ',
935 'Œ' => 'Œ',
936 'œ' => 'œ',
937 'Š' => 'Š',
938 'š' => 'š',
939 'Ÿ' => 'Ÿ',
940 'ƒ' => 'ƒ',
941 'ˆ' => 'ˆ',
942 '˜' => '˜',
943 'Α' => 'Α',
944 'Β' => 'Β',
945 'Γ' => 'Γ',
946 'Δ' => 'Δ',
947 'Ε' => 'Ε',
948 'Ζ' => 'Ζ',
950 'Θ' => 'Θ',
951 'Ι' => 'Ι',
952 'Κ' => 'Κ',
953 'Λ' => 'Λ',
957 'Ο' => 'Ο',
960 'Σ' => 'Σ',
962 'Υ' => 'Υ',
966 'Ω' => 'Ω',
967 'α' => 'α',
968 'β' => 'β',
969 'γ' => 'γ',
970 'δ' => 'δ',
971 'ε' => 'ε',
972 'ζ' => 'ζ',
974 'θ' => 'θ',
975 'ι' => 'ι',
976 'κ' => 'κ',
977 'λ' => 'λ',
981 'ο' => 'ο',
984 'ς' => 'ς',
985 'σ' => 'σ',
987 'υ' => 'υ',
991 'ω' => 'ω',
992 'ϑ' => 'ϑ',
993 'ϒ' => 'ϒ',
995 ' ' => ' ',
996 ' ' => ' ',
997 ' ' => ' ',
998 '‌' => '‌',
999 '‍' => '‍',
1000 '‎' => '‎',
1001 '‏' => '‏',
1002 '–' => '–',
1003 '—' => '—',
1004 '‘' => '‘',
1005 '’' => '’',
1006 '‚' => '‚',
1007 '“' => '“',
1008 '”' => '”',
1009 '„' => '„',
1010 '†' => '†',
1011 '‡' => '‡',
1012 '•' => '•',
1013 '…' => '…',
1014 '‰' => '‰',
1015 '′' => '′',
1016 '″' => '″',
1017 '‹' => '‹',
1018 '›' => '›',
1019 '‾' => '‾',
1020 '⁄' => '⁄',
1021 '€' => '€',
1022 'ℑ' => 'ℑ',
1023 '℘' => '℘',
1024 'ℜ' => 'ℜ',
1025 '™' => '™',
1026 'ℵ' => 'ℵ',
1027 '↵' => '↵',
1028 '⇐' => '⇐',
1029 '⇑' => '⇑',
1030 '⇒' => '⇒',
1031 '⇓' => '⇓',
1032 '⇔' => '⇔',
1033 '∀' => '∀',
1034 '∂' => '∂',
1035 '∃' => '∃',
1036 '∅' => '∅',
1037 '∇' => '∇',
1038 '∈' => '∈',
1039 '∉' => '∉',
1040 '∋' => '∋',
1041 '∏' => '∏',
1042 '∑' => '∑',
1043 '−' => '−',
1044 '∗' => '∗',
1045 '√' => '√',
1046 '∝' => '∝',
1047 '∞' => '∞',
1048 '∠' => '∠',
1049 '∧' => '∧',
1050 '∨' => '∨',
1051 '∩' => '∩',
1052 '∪' => '∪',
1053 '∫' => '∫',
1054 '∴' => '∴',
1055 '∼' => '∼',
1056 '≅' => '≅',
1057 '≈' => '≈',
1058 '≠' => '≠',
1059 '≡' => '≡',
1060 '≤' => '≤',
1061 '≥' => '≥',
1062 '⊂' => '⊂',
1063 '⊃' => '⊃',
1064 '⊄' => '⊄',
1065 '⊆' => '⊆',
1066 '⊇' => '⊇',
1067 '⊕' => '⊕',
1068 '⊗' => '⊗',
1069 '⊥' => '⊥',
1070 '⋅' => '⋅',
1071 '⌈' => '⌈',
1072 '⌉' => '⌉',
1073 '⌊' => '⌊',
1074 '⌋' => '⌋',
1075 '⟨' => '〈',
1076 '⟩' => '〉',
1077 '←' => '←',
1078 '↑' => '↑',
1079 '→' => '→',
1080 '↓' => '↓',
1081 '↔' => '↔',
1082 '◊' => '◊',
1083 '♠' => '♠',
1084 '♣' => '♣',
1085 '♥' => '♥',
1086 '♦' => '♦'
1089 return str_replace( array_keys($to_ncr), array_values($to_ncr), $text );
1092 function wp_richedit_pre($text) {
1093 // Filtering a blank results in an annoying <br />\n
1094 if ( empty($text) ) return apply_filters('richedit_pre', '');
1097 $output = convert_chars($output);
1098 $output = wpautop($output);
1100 // These must be double-escaped or planets will collide.
1101 $output = str_replace('<', '&lt;', $output);
1102 $output = str_replace('>', '&gt;', $output);
1104 return apply_filters('richedit_pre', $output);
1107 function clean_url( $url, $protocols = null, $context = 'display' ) {
1108 $original_url = $url;
1110 if ('' == $url) return $url;
1111 $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@]|i', '', $url);
1112 $strip = array('%0d', '%0a');
1113 $url = str_replace($strip, '', $url);
1114 $url = str_replace(';//', '://', $url);
1115 /* If the URL doesn't appear to contain a scheme, we
1116 * presume it needs http:// appended (unless a relative
1117 * link starting with / or a php file).
1119 if ( strpos($url, ':') === false &&
1120 substr( $url, 0, 1 ) != '/' && !preg_match('/^[a-z0-9-]+?\.php/i', $url) )
1121 $url = 'http://' . $url;
1123 // Replace ampersands ony when displaying.
1124 if ( 'display' == $context )
1125 $url = preg_replace('/&([^#])(?![a-z]{2,8};)/', '&$1', $url);
1127 if ( !is_array($protocols) )
1128 $protocols = array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet');
1129 if ( wp_kses_bad_protocol( $url, $protocols ) != $url )
1132 return apply_filters('clean_url', $url, $original_url, $context);
1135 function sanitize_url( $url, $protocols = null ) {
1136 return clean_url( $url, $protocols, 'db');
1139 // Borrowed from the PHP Manual user notes. Convert entities, while
1140 // preserving already-encoded entities:
1141 function htmlentities2($myHTML) {
1142 $translation_table=get_html_translation_table (HTML_ENTITIES,ENT_QUOTES);
1143 $translation_table[chr(38)] = '&';
1144 return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/","&" , strtr($myHTML, $translation_table));
1147 // Escape single quotes, specialchar double quotes, and fix line endings.
1148 function js_escape($text) {
1149 $safe_text = wp_specialchars($text, 'double');
1150 $safe_text = preg_replace('/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes($safe_text));
1151 $safe_text = preg_replace("/\r?\n/", "\\n", addslashes($safe_text));
1152 return apply_filters('js_escape', $safe_text, $text);
1155 // Escaping for HTML attributes
1156 function attribute_escape($text) {
1157 $safe_text = wp_specialchars($text, true);
1158 return apply_filters('attribute_escape', $safe_text, $text);
1161 function wp_make_link_relative( $link ) {
1162 return preg_replace('|https?://[^/]+(/.*)|i', '$1', $link );
1165 function sanitize_option($option, $value) { // Remember to call stripslashes!
1169 $value = sanitize_email($value);
1172 case 'default_post_edit_rows':
1173 case 'mailserver_port':
1174 case 'comment_max_links':
1175 case 'page_on_front':
1176 case 'rss_excerpt_length':
1177 case 'default_category':
1178 case 'default_email_category':
1179 case 'default_link_category':
1180 $value = abs((int) $value);
1183 case 'posts_per_page':
1184 case 'posts_per_rss':
1185 $value = (int) $value;
1186 if ( empty($value) ) $value = 1;
1187 if ( $value < -1 ) $value = abs($value);
1190 case 'default_ping_status':
1191 case 'default_comment_status':
1192 // Options that if not there have 0 value but need to be something like "closed"
1193 if ( $value == '0' || $value == '')
1197 case 'blogdescription':
1199 $value = addslashes($value);
1200 $value = wp_filter_post_kses( $value ); // calls stripslashes then addslashes
1201 $value = stripslashes($value);
1202 $value = wp_specialchars( $value );
1205 case 'blog_charset':
1206 $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value); // strips slashes
1211 case 'mailserver_url':
1212 case 'mailserver_login':
1213 case 'mailserver_pass':
1216 $value = strip_tags($value);
1217 $value = addslashes($value);
1218 $value = wp_filter_kses($value); // calls stripslashes then addslashes
1219 $value = stripslashes($value);
1223 $value = preg_replace('/[^0-9:.-]/', '', $value); // strips slashes
1228 $value = stripslashes($value);
1229 $value = clean_url($value);
1232 $value = apply_filters("sanitize_option_{$option}", $value, $option);
1239 function wp_parse_str( $string, &$array ) {
1240 parse_str( $string, $array );
1241 if ( get_magic_quotes_gpc() )
1242 $array = stripslashes_deep( $array ); // parse_str() adds slashes if magicquotes is on. See: http://php.net/parse_str
1243 $array = apply_filters( 'wp_parse_str', $array );
1246 // Convert lone less than signs. KSES already converts lone greater than signs.
1247 function wp_pre_kses_less_than( $text ) {
1248 return preg_replace_callback('%<[^>]*?((?=<)|>|$)%', 'wp_pre_kses_less_than_callback', $text);
1251 function wp_pre_kses_less_than_callback( $matches ) {
1252 if ( false === strpos($matches[0], '>') )
1253 return wp_specialchars($matches[0]);