3 function wptexturize($text) {
4 global $wp_cockneyreplace;
6 $has_pre_parent = false;
9 $textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
10 $stop = count($textarr);
12 // if a plugin has provided an autocorrect array, use it
13 if ( isset($wp_cockneyreplace) ) {
14 $cockney = array_keys($wp_cockneyreplace);
15 $cockneyreplace = array_values($wp_cockneyreplace);
17 $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");
18 $cockneyreplace = array("’tain’t","’twere","’twas","’tis","’twill","’til","’bout","’nuff","’round","’cause");
21 $static_characters = array_merge(array('---', ' -- ', '--', 'xn–', '...', '``', '\'s', '\'\'', ' (tm)'), $cockney);
22 $static_replacements = array_merge(array('—', ' — ', '–', 'xn--', '…', '“', '’s', '”', ' ™'), $cockneyreplace);
24 $dynamic_characters = array('/\'(\d\d(?:’|\')?s)/', '/(\s|\A|")\'/', '/(\d+)"/', '/(\d+)\'/', '/(\S)\'([^\'\s])/', '/(\s|\A)"(?!\s)/', '/"(\s|\S|\Z)/', '/\'([\s.]|\Z)/', '/(\d+)x(\d+)/');
25 $dynamic_replacements = array('’$1','$1‘', '$1″', '$1′', '$1’$2', '$1“$2', '”$1', '’$1', '$1×$2');
27 for ( $i = 0; $i < $stop; $i++ ) {
30 if (isset($curl{0}) && '<' != $curl{0} && '[' != $curl{0} && $next && !$has_pre_parent) { // If it's not a tag
32 $curl = str_replace($static_characters, $static_replacements, $curl);
33 // regular expressions
34 $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
35 } elseif (strpos($curl, '<code') !== false || strpos($curl, '<kbd') !== false || strpos($curl, '<style') !== false || strpos($curl, '<script') !== false) {
37 } elseif (strpos($curl, '<pre') !== false) {
38 $has_pre_parent = true;
39 } elseif (strpos($curl, '</pre>') !== false) {
40 $has_pre_parent = false;
45 $curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&$1', $curl);
52 // Accepts matches array from preg_replace_callback in wpautop()
54 function clean_pre($matches) {
55 if ( is_array($matches) )
56 $text = $matches[1] . $matches[2] . "</pre>";
60 $text = str_replace('<br />', '', $text);
61 $text = str_replace('<p>', "\n", $text);
62 $text = str_replace('</p>', '', $text);
67 function wpautop($pee, $br = 1) {
68 $pee = $pee . "\n"; // just to make things a little easier, pad the end
69 $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
70 // Space things out a little
71 $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)';
72 $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
73 $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
74 $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
75 if ( strpos($pee, '<object') !== false ) {
76 $pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed
77 $pee = preg_replace('|\s*</embed>\s*|', '</embed>', $pee);
79 $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
80 $pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $pee); // make paragraphs, including one at the end
81 $pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
82 $pee = preg_replace('!<p>([^<]+)\s*?(</(?:div|address|form)[^>]*>)!', "<p>$1</p>$2", $pee);
83 $pee = preg_replace( '|<p>|', "$1<p>", $pee );
84 $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
85 $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
86 $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
87 $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
88 $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
89 $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
91 $pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', create_function('$matches', 'return str_replace("\n", "<WPPreserveNewline />", $matches[0]);'), $pee);
92 $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
93 $pee = str_replace('<WPPreserveNewline />', "\n", $pee);
95 $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
96 $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
97 if (strpos($pee, '<pre') !== false)
98 $pee = preg_replace_callback('!(<pre.*?>)(.*?)</pre>!is', 'clean_pre', $pee );
99 $pee = preg_replace( "|\n</p>$|", '</p>', $pee );
100 $pee = preg_replace('/<p>\s*?(' . get_shortcode_regex() . ')\s*<\/p>/s', '$1', $pee); // don't auto-p wrap shortcodes that stand alone
106 function seems_utf8($Str) { # by bmorel at ssi dot fr
107 $length = strlen($Str);
108 for ($i=0; $i < $length; $i++) {
109 if (ord($Str[$i]) < 0x80) continue; # 0bbbbbbb
110 elseif ((ord($Str[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
111 elseif ((ord($Str[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
112 elseif ((ord($Str[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb
113 elseif ((ord($Str[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb
114 elseif ((ord($Str[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
115 else return false; # Does not match any model
116 for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
117 if ((++$i == $length) || ((ord($Str[$i]) & 0xC0) != 0x80))
124 function wp_specialchars( $text, $quotes = 0 ) {
125 // Like htmlspecialchars except don't double-encode HTML entities
126 $text = str_replace('&&', '&&', $text);
127 $text = str_replace('&&', '&&', $text);
128 $text = preg_replace('/&(?:$|([^#])(?![a-z1-4]{1,8};))/', '&$1', $text);
129 $text = str_replace('<', '<', $text);
130 $text = str_replace('>', '>', $text);
131 if ( 'double' === $quotes ) {
132 $text = str_replace('"', '"', $text);
133 } elseif ( 'single' === $quotes ) {
134 $text = str_replace("'", ''', $text);
135 } elseif ( $quotes ) {
136 $text = str_replace('"', '"', $text);
137 $text = str_replace("'", ''', $text);
142 function utf8_uri_encode( $utf8_string, $length = 0 ) {
148 $string_length = strlen( $utf8_string );
149 for ($i = 0; $i < $string_length; $i++ ) {
151 $value = ord( $utf8_string[ $i ] );
153 if ( $value < 128 ) {
154 if ( $length && ( $unicode_length >= $length ) )
156 $unicode .= chr($value);
159 if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3;
163 if ( $length && ( $unicode_length + ($num_octets * 3) ) > $length )
165 if ( count( $values ) == $num_octets ) {
166 if ($num_octets == 3) {
167 $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]);
168 $unicode_length += 9;
170 $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]);
171 $unicode_length += 6;
183 function remove_accents($string) {
184 if ( !preg_match('/[\x80-\xff]/', $string) )
187 if (seems_utf8($string)) {
189 // Decompositions for Latin-1 Supplement
190 chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
191 chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
192 chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
193 chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
194 chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
195 chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
196 chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
197 chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
198 chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
199 chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
200 chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
201 chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
202 chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
203 chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
204 chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
205 chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
206 chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
207 chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
208 chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
209 chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
210 chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
211 chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
212 chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
213 chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
214 chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
215 chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
216 chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
217 chr(195).chr(191) => 'y',
218 // Decompositions for Latin Extended-A
219 chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
220 chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
221 chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
222 chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
223 chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
224 chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
225 chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
226 chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
227 chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
228 chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
229 chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
230 chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
231 chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
232 chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
233 chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
234 chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
235 chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
236 chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
237 chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
238 chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
239 chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
240 chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
241 chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
242 chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
243 chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
244 chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
245 chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
246 chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
247 chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
248 chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
249 chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
250 chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
251 chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
252 chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
253 chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
254 chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
255 chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
256 chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
257 chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
258 chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
259 chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
260 chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
261 chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
262 chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
263 chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
264 chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
265 chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
266 chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
267 chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
268 chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
269 chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
270 chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
271 chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
272 chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
273 chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
274 chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
275 chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
276 chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
277 chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
278 chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
279 chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
280 chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
281 chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
282 chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
284 chr(226).chr(130).chr(172) => 'E',
286 chr(194).chr(163) => '');
288 $string = strtr($string, $chars);
290 // Assume ISO-8859-1 if not UTF-8
291 $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
292 .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
293 .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
294 .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
295 .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
296 .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
297 .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
298 .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
299 .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
300 .chr(252).chr(253).chr(255);
302 $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
304 $string = strtr($string, $chars['in'], $chars['out']);
305 $double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
306 $double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
307 $string = str_replace($double_chars['in'], $double_chars['out'], $string);
313 function sanitize_file_name( $name ) { // Like sanitize_title, but with periods
314 $name = strtolower( $name );
315 $name = preg_replace('/&.+?;/', '', $name); // kill entities
316 $name = str_replace( '_', '-', $name );
317 $name = preg_replace('/[^a-z0-9\s-.]/', '', $name);
318 $name = preg_replace('/\s+/', '-', $name);
319 $name = preg_replace('|-+|', '-', $name);
320 $name = trim($name, '-');
324 function sanitize_user( $username, $strict = false ) {
325 $raw_username = $username;
326 $username = strip_tags($username);
328 $username = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '', $username);
329 $username = preg_replace('/&.+?;/', '', $username); // Kill entities
331 // If strict, reduce to ASCII for max portability.
333 $username = preg_replace('|[^a-z0-9 _.\-@]|i', '', $username);
335 // Consolidate contiguous whitespace
336 $username = preg_replace('|\s+|', ' ', $username);
338 return apply_filters('sanitize_user', $username, $raw_username, $strict);
341 function sanitize_title($title, $fallback_title = '') {
342 $title = strip_tags($title);
343 $title = apply_filters('sanitize_title', $title);
345 if ( '' === $title || false === $title )
346 $title = $fallback_title;
351 function sanitize_title_with_dashes($title) {
352 $title = strip_tags($title);
353 // Preserve escaped octets.
354 $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
355 // Remove percent signs that are not part of an octet.
356 $title = str_replace('%', '', $title);
358 $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
360 $title = remove_accents($title);
361 if (seems_utf8($title)) {
362 if (function_exists('mb_strtolower')) {
363 $title = mb_strtolower($title, 'UTF-8');
365 $title = utf8_uri_encode($title, 200);
368 $title = strtolower($title);
369 $title = preg_replace('/&.+?;/', '', $title); // kill entities
370 $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
371 $title = preg_replace('/\s+/', '-', $title);
372 $title = preg_replace('|-+|', '-', $title);
373 $title = trim($title, '-');
378 // ensures a string is a valid SQL order by clause like: post_name ASC, ID DESC
379 // accepts one or more columns, with or without ASC/DESC, and also accepts RAND()
380 function sanitize_sql_orderby( $orderby ){
381 preg_match('/^\s*([a-z0-9_]+(\s+(ASC|DESC))?(\s*,\s*|\s*$))+|^\s*RAND\(\s*\)\s*$/i', $orderby, $obmatches);
387 function convert_chars($content, $deprecated = '') {
388 // Translation of invalid Unicode references range to valid range
389 $wp_htmltranswinuni = array(
390 '€' => '€', // the Euro sign
392 '‚' => '‚', // these are Windows CP1252 specific characters
393 'ƒ' => 'ƒ', // they would look weird on non-Windows browsers
394 '„' => '„',
395 '…' => '…',
396 '†' => '†',
397 '‡' => '‡',
398 'ˆ' => 'ˆ',
399 '‰' => '‰',
400 'Š' => 'Š',
401 '‹' => '‹',
402 'Œ' => 'Œ',
404 'Ž' => 'ž',
407 '‘' => '‘',
408 '’' => '’',
409 '“' => '“',
410 '”' => '”',
411 '•' => '•',
412 '–' => '–',
413 '—' => '—',
414 '˜' => '˜',
415 '™' => '™',
416 'š' => 'š',
417 '›' => '›',
418 'œ' => 'œ',
424 // Remove metadata tags
425 $content = preg_replace('/<title>(.+?)<\/title>/','',$content);
426 $content = preg_replace('/<category>(.+?)<\/category>/','',$content);
428 // Converts lone & characters into & (a.k.a. &)
429 $content = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/i', '&$1', $content);
432 $content = strtr($content, $wp_htmltranswinuni);
434 // Just a little XHTML help
435 $content = str_replace('<br>', '<br />', $content);
436 $content = str_replace('<hr>', '<hr />', $content);
441 function funky_javascript_fix($text) {
442 // Fixes for browsers' javascript bugs
443 global $is_macIE, $is_winIE;
445 if ( $is_winIE || $is_macIE )
446 $text = preg_replace("/\%u([0-9A-F]{4,4})/e", "'&#'.base_convert('\\1',16,10).';'", $text);
451 function balanceTags( $text, $force = false ) {
452 if ( !$force && get_option('use_balanceTags') == 0 )
454 return force_balance_tags( $text );
460 Balances Tags of string using a modified stack.
462 @param text Text to be balanced
463 @param force Forces balancing, ignoring the value of the option
464 @return Returns balanced text
465 @author Leonard Lin (leonard@acm.org)
467 @date November 4, 2001
471 --- Modified by Scott Reilly (coffee2code) 02 Aug 2004
472 1.2 ***TODO*** Make better - change loop condition to $text
473 1.1 Fixed handling of append/stack pop order of end text
477 function force_balance_tags( $text ) {
478 $tagstack = array(); $stacksize = 0; $tagqueue = ''; $newtext = '';
479 $single_tags = array('br', 'hr', 'img', 'input'); //Known single-entity/self-closing tags
480 $nestable_tags = array('blockquote', 'div', 'span'); //Tags that can be immediately nested within themselves
482 # WP bug fix for comments - in case you REALLY meant to type '< !--'
483 $text = str_replace('< !--', '< !--', $text);
484 # WP bug fix for LOVE <3 (and other situations with '<' before a number)
485 $text = preg_replace('#<([0-9]{1})#', '<$1', $text);
487 while (preg_match("/<(\/?\w*)\s*([^>]*)>/",$text,$regex)) {
488 $newtext .= $tagqueue;
490 $i = strpos($text,$regex[0]);
491 $l = strlen($regex[0]);
496 if ($regex[1][0] == "/") { // End Tag
497 $tag = strtolower(substr($regex[1],1));
498 // if too many closing tags
499 if($stacksize <= 0) {
501 //or close to be safe $tag = '/' . $tag;
503 // if stacktop value = tag close value then pop
504 else if ($tagstack[$stacksize - 1] == $tag) { // found closing tag
505 $tag = '</' . $tag . '>'; // Close Tag
507 array_pop ($tagstack);
509 } else { // closing tag not at top, search for it
510 for ($j=$stacksize-1;$j>=0;$j--) {
511 if ($tagstack[$j] == $tag) {
512 // add tag to tagqueue
513 for ($k=$stacksize-1;$k>=$j;$k--){
514 $tagqueue .= '</' . array_pop ($tagstack) . '>';
522 } else { // Begin Tag
523 $tag = strtolower($regex[1]);
527 // If self-closing or '', don't do anything.
528 if((substr($regex[2],-1) == '/') || ($tag == '')) {
530 // ElseIf it's a known single-entity tag but it doesn't close itself, do so
531 elseif ( in_array($tag, $single_tags) ) {
533 } else { // Push the tag onto the stack
534 // If the top of the stack is the same as the tag we want to push, close previous tag
535 if (($stacksize > 0) && !in_array($tag, $nestable_tags) && ($tagstack[$stacksize - 1] == $tag)) {
536 $tagqueue = '</' . array_pop ($tagstack) . '>';
539 $stacksize = array_push ($tagstack, $tag);
543 $attributes = $regex[2];
545 $attributes = ' '.$attributes;
547 $tag = '<'.$tag.$attributes.'>';
548 //If already queuing a close tag, then put this tag on, too
554 $newtext .= substr($text,0,$i) . $tag;
555 $text = substr($text,$i+$l);
559 $newtext .= $tagqueue;
561 // Add Remaining text
565 while($x = array_pop($tagstack)) {
566 $newtext .= '</' . $x . '>'; // Add remaining tags to close
569 // WP fix for the bug with HTML comments
570 $newtext = str_replace("< !--","<!--",$newtext);
571 $newtext = str_replace("< !--","< !--",$newtext);
576 function format_to_edit($content, $richedit = false) {
577 $content = apply_filters('format_to_edit', $content);
579 $content = htmlspecialchars($content);
583 function format_to_post($content) {
584 $content = apply_filters('format_to_post', $content);
588 function zeroise($number,$threshold) { // function to add leading zeros when necessary
589 return sprintf('%0'.$threshold.'s', $number);
593 function backslashit($string) {
594 $string = preg_replace('/^([0-9])/', '\\\\\\\\\1', $string);
595 $string = preg_replace('/([a-z])/i', '\\\\\1', $string);
599 function trailingslashit($string) {
600 return untrailingslashit($string) . '/';
603 function untrailingslashit($string) {
604 return rtrim($string, '/');
607 function addslashes_gpc($gpc) {
610 if (get_magic_quotes_gpc()) {
611 $gpc = stripslashes($gpc);
614 return $wpdb->escape($gpc);
618 function stripslashes_deep($value) {
619 $value = is_array($value) ?
620 array_map('stripslashes_deep', $value) :
621 stripslashes($value);
626 function urlencode_deep($value) {
627 $value = is_array($value) ?
628 array_map('urlencode_deep', $value) :
634 function antispambot($emailaddy, $mailto=0) {
635 $emailNOSPAMaddy = '';
636 srand ((float) microtime() * 1000000);
637 for ($i = 0; $i < strlen($emailaddy); $i = $i + 1) {
638 $j = floor(rand(0, 1+$mailto));
640 $emailNOSPAMaddy .= '&#'.ord(substr($emailaddy,$i,1)).';';
642 $emailNOSPAMaddy .= substr($emailaddy,$i,1);
644 $emailNOSPAMaddy .= '%'.zeroise(dechex(ord(substr($emailaddy, $i, 1))), 2);
647 $emailNOSPAMaddy = str_replace('@','@',$emailNOSPAMaddy);
648 return $emailNOSPAMaddy;
651 function _make_url_clickable_cb($matches) {
654 $url = clean_url($url);
657 // removed trailing [.,;:] from URL
658 if ( in_array(substr($url, -1), array('.', ',', ';', ':')) === true ) {
659 $ret = substr($url, -1);
660 $url = substr($url, 0, strlen($url)-1);
662 return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>" . $ret;
665 function _make_web_ftp_clickable_cb($matches) {
668 $dest = 'http://' . $dest;
669 $dest = clean_url($dest);
672 // removed trailing [,;:] from URL
673 if ( in_array(substr($dest, -1), array('.', ',', ';', ':')) === true ) {
674 $ret = substr($dest, -1);
675 $dest = substr($dest, 0, strlen($dest)-1);
677 return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>" . $ret;
680 function _make_email_clickable_cb($matches) {
681 $email = $matches[2] . '@' . $matches[3];
682 return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
685 function make_clickable($ret) {
687 // in testing, using arrays here was found to be faster
688 $ret = preg_replace_callback('#([\s>])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_url_clickable_cb', $ret);
689 $ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_web_ftp_clickable_cb', $ret);
690 $ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret);
691 // this one is not in an array because we need it to run last, for cleanup of accidental links within links
692 $ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);
697 function wp_rel_nofollow( $text ) {
699 // This is a pre save filter, so text is already escaped.
700 $text = stripslashes($text);
701 $text = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $text);
702 $text = $wpdb->escape($text);
706 function wp_rel_nofollow_callback( $matches ) {
708 $text = str_replace(array(' rel="nofollow"', " rel='nofollow'"), '', $text);
709 return "<a $text rel=\"nofollow\">";
712 function convert_smilies($text) {
713 global $wp_smiliessearch, $wp_smiliesreplace;
715 if ( get_option('use_smilies') && !empty($wp_smiliessearch) && !empty($wp_smiliesreplace) ) {
716 // HTML loop taken from texturize function, could possible be consolidated
717 $textarr = preg_split("/(<.*>)/U", $text, -1, PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between
718 $stop = count($textarr);// loop stuff
719 for ($i = 0; $i < $stop; $i++) {
720 $content = $textarr[$i];
721 if ((strlen($content) > 0) && ('<' != $content{0})) { // If it's not a tag
722 $content = preg_replace($wp_smiliessearch, $wp_smiliesreplace, $content);
727 // return default text.
734 function is_email($user_email) {
735 $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
736 if (strpos($user_email, '@') !== false && strpos($user_email, '.') !== false) {
737 if (preg_match($chars, $user_email)) {
747 // used by wp-mail to handle charsets in email subjects
748 function wp_iso_descrambler($string) {
749 /* this may only work with iso-8859-1, I'm afraid */
750 if (!preg_match('#\=\?(.+)\?Q\?(.+)\?\=#i', $string, $matches)) {
753 $subject = str_replace('_', ' ', $matches[2]);
754 $subject = preg_replace('#\=([0-9a-f]{2})#ei', "chr(hexdec(strtolower('$1')))", $subject);
760 // give it a date, it will give you the same date as GMT
761 function get_gmt_from_date($string) {
762 // note: this only substracts $time_difference from the given date
763 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);
764 $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
765 $string_gmt = gmdate('Y-m-d H:i:s', $string_time - get_option('gmt_offset') * 3600);
769 // give it a GMT date, it will give you the same date with $time_difference added
770 function get_date_from_gmt($string) {
771 // note: this only adds $time_difference to the given date
772 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);
773 $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
774 $string_localtime = gmdate('Y-m-d H:i:s', $string_time + get_option('gmt_offset')*3600);
775 return $string_localtime;
778 // computes an offset in seconds from an iso8601 timezone
779 function iso8601_timezone_to_offset($timezone) {
780 // $timezone is either 'Z' or '[+|-]hhmm'
781 if ($timezone == 'Z') {
784 $sign = (substr($timezone, 0, 1) == '+') ? 1 : -1;
785 $hours = intval(substr($timezone, 1, 2));
786 $minutes = intval(substr($timezone, 3, 4)) / 60;
787 $offset = $sign * 3600 * ($hours + $minutes);
792 // converts an iso8601 date to MySQL DateTime format used by post_date[_gmt]
793 function iso8601_to_datetime($date_string, $timezone = USER) {
794 if ($timezone == GMT) {
795 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);
796 if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset
797 $offset = iso8601_timezone_to_offset($date_bits[7]);
798 } else { // we don't have a timezone, so we assume user local timezone (not server's!)
799 $offset = 3600 * get_option('gmt_offset');
801 $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
802 $timestamp -= $offset;
803 return gmdate('Y-m-d H:i:s', $timestamp);
804 } elseif ($timezone == USER) {
805 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);
809 function popuplinks($text) {
810 // Comment text in popup windows should be filtered through this.
811 // Right now it's a moderately dumb function, ideally it would detect whether
812 // a target or rel attribute was already there and adjust its actions accordingly.
813 $text = preg_replace('/<a (.+?)>/i', "<a $1 target='_blank' rel='external'>", $text);
817 function sanitize_email($email) {
818 return preg_replace('/[^a-z0-9+_.@-]/i', '', $email);
821 function human_time_diff( $from, $to = '' ) {
824 $diff = (int) abs($to - $from);
826 $mins = round($diff / 60);
830 $since = sprintf(__ngettext('%s min', '%s mins', $mins), $mins);
831 } else if (($diff <= 86400) && ($diff > 3600)) {
832 $hours = round($diff / 3600);
836 $since = sprintf(__ngettext('%s hour', '%s hours', $hours), $hours);
837 } elseif ($diff >= 86400) {
838 $days = round($diff / 86400);
842 $since = sprintf(__ngettext('%s day', '%s days', $days), $days);
847 function wp_trim_excerpt($text) { // Fakes an excerpt if needed
849 $text = get_the_content('');
851 $text = strip_shortcodes( $text );
853 $text = apply_filters('the_content', $text);
854 $text = str_replace(']]>', ']]>', $text);
855 $text = strip_tags($text);
856 $excerpt_length = 55;
857 $words = explode(' ', $text, $excerpt_length + 1);
858 if (count($words) > $excerpt_length) {
860 array_push($words, '[...]');
861 $text = implode(' ', $words);
867 function ent2ncr($text) {
871 '⁄' => '/',
875 ' ' => ' ',
876 '¡' => '¡',
877 '¢' => '¢',
878 '£' => '£',
879 '¤' => '¤',
881 '¦' => '¦',
882 '&brkbar;' => '¦',
883 '§' => '§',
886 '©' => '©',
887 'ª' => 'ª',
888 '«' => '«',
892 '¯' => '¯',
893 '&hibar;' => '¯',
895 '±' => '±',
896 '²' => '²',
897 '³' => '³',
898 '´' => '´',
899 'µ' => 'µ',
900 '¶' => '¶',
901 '·' => '·',
902 '¸' => '¸',
903 '¹' => '¹',
904 'º' => 'º',
905 '»' => '»',
906 '¼' => '¼',
907 '½' => '½',
908 '¾' => '¾',
909 '¿' => '¿',
910 'À' => 'À',
911 'Á' => 'Á',
912 'Â' => 'Â',
913 'Ã' => 'Ã',
914 'Ä' => 'Ä',
915 'Å' => 'Å',
916 'Æ' => 'Æ',
917 'Ç' => 'Ç',
918 'È' => 'È',
919 'É' => 'É',
920 'Ê' => 'Ê',
921 'Ë' => 'Ë',
922 'Ì' => 'Ì',
923 'Í' => 'Í',
924 'Î' => 'Î',
925 'Ï' => 'Ï',
927 'Ñ' => 'Ñ',
928 'Ò' => 'Ò',
929 'Ó' => 'Ó',
930 'Ô' => 'Ô',
931 'Õ' => 'Õ',
932 'Ö' => 'Ö',
933 '×' => '×',
934 'Ø' => 'Ø',
935 'Ù' => 'Ù',
936 'Ú' => 'Ú',
937 'Û' => 'Û',
938 'Ü' => 'Ü',
939 'Ý' => 'Ý',
940 'Þ' => 'Þ',
941 'ß' => 'ß',
942 'à' => 'à',
943 'á' => 'á',
944 'â' => 'â',
945 'ã' => 'ã',
946 'ä' => 'ä',
947 'å' => 'å',
948 'æ' => 'æ',
949 'ç' => 'ç',
950 'è' => 'è',
951 'é' => 'é',
952 'ê' => 'ê',
953 'ë' => 'ë',
954 'ì' => 'ì',
955 'í' => 'í',
956 'î' => 'î',
957 'ï' => 'ï',
959 'ñ' => 'ñ',
960 'ò' => 'ò',
961 'ó' => 'ó',
962 'ô' => 'ô',
963 'õ' => 'õ',
964 'ö' => 'ö',
965 '÷' => '÷',
966 'ø' => 'ø',
967 'ù' => 'ù',
968 'ú' => 'ú',
969 'û' => 'û',
970 'ü' => 'ü',
971 'ý' => 'ý',
972 'þ' => 'þ',
973 'ÿ' => 'ÿ',
974 'Œ' => 'Œ',
975 'œ' => 'œ',
976 'Š' => 'Š',
977 'š' => 'š',
978 'Ÿ' => 'Ÿ',
979 'ƒ' => 'ƒ',
980 'ˆ' => 'ˆ',
981 '˜' => '˜',
982 'Α' => 'Α',
983 'Β' => 'Β',
984 'Γ' => 'Γ',
985 'Δ' => 'Δ',
986 'Ε' => 'Ε',
987 'Ζ' => 'Ζ',
989 'Θ' => 'Θ',
990 'Ι' => 'Ι',
991 'Κ' => 'Κ',
992 'Λ' => 'Λ',
996 'Ο' => 'Ο',
999 'Σ' => 'Σ',
1000 'Τ' => 'Τ',
1001 'Υ' => 'Υ',
1002 'Φ' => 'Φ',
1003 'Χ' => 'Χ',
1004 'Ψ' => 'Ψ',
1005 'Ω' => 'Ω',
1006 'α' => 'α',
1007 'β' => 'β',
1008 'γ' => 'γ',
1009 'δ' => 'δ',
1010 'ε' => 'ε',
1011 'ζ' => 'ζ',
1012 'η' => 'η',
1013 'θ' => 'θ',
1014 'ι' => 'ι',
1015 'κ' => 'κ',
1016 'λ' => 'λ',
1020 'ο' => 'ο',
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 '∞' => '∞',
1087 '∠' => '∠',
1088 '∧' => '∧',
1089 '∨' => '∨',
1090 '∩' => '∩',
1091 '∪' => '∪',
1092 '∫' => '∫',
1093 '∴' => '∴',
1094 '∼' => '∼',
1095 '≅' => '≅',
1096 '≈' => '≈',
1097 '≠' => '≠',
1098 '≡' => '≡',
1099 '≤' => '≤',
1100 '≥' => '≥',
1101 '⊂' => '⊂',
1102 '⊃' => '⊃',
1103 '⊄' => '⊄',
1104 '⊆' => '⊆',
1105 '⊇' => '⊇',
1106 '⊕' => '⊕',
1107 '⊗' => '⊗',
1108 '⊥' => '⊥',
1109 '⋅' => '⋅',
1110 '⌈' => '⌈',
1111 '⌉' => '⌉',
1112 '⌊' => '⌊',
1113 '⌋' => '⌋',
1114 '⟨' => '〈',
1115 '⟩' => '〉',
1116 '←' => '←',
1117 '↑' => '↑',
1118 '→' => '→',
1119 '↓' => '↓',
1120 '↔' => '↔',
1121 '◊' => '◊',
1122 '♠' => '♠',
1123 '♣' => '♣',
1124 '♥' => '♥',
1125 '♦' => '♦'
1128 return str_replace( array_keys($to_ncr), array_values($to_ncr), $text );
1131 function wp_richedit_pre($text) {
1132 // Filtering a blank results in an annoying <br />\n
1133 if ( empty($text) ) return apply_filters('richedit_pre', '');
1135 $output = convert_chars($text);
1136 $output = wpautop($output);
1137 $output = htmlspecialchars($output, ENT_NOQUOTES);
1139 return apply_filters('richedit_pre', $output);
1142 function wp_htmledit_pre($output) {
1143 if ( !empty($output) )
1144 $output = htmlspecialchars($output, ENT_NOQUOTES); // convert only < > &
1146 return apply_filters('htmledit_pre', $output);
1149 function clean_url( $url, $protocols = null, $context = 'display' ) {
1150 $original_url = $url;
1152 if ('' == $url) return $url;
1153 $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@()]|i', '', $url);
1154 $strip = array('%0d', '%0a');
1155 $url = str_replace($strip, '', $url);
1156 $url = str_replace(';//', '://', $url);
1157 /* If the URL doesn't appear to contain a scheme, we
1158 * presume it needs http:// appended (unless a relative
1159 * link starting with / or a php file).
1161 if ( strpos($url, ':') === false &&
1162 substr( $url, 0, 1 ) != '/' && !preg_match('/^[a-z0-9-]+?\.php/i', $url) )
1163 $url = 'http://' . $url;
1165 // Replace ampersands ony when displaying.
1166 if ( 'display' == $context )
1167 $url = preg_replace('/&([^#])(?![a-z]{2,8};)/', '&$1', $url);
1169 if ( !is_array($protocols) )
1170 $protocols = array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet');
1171 if ( wp_kses_bad_protocol( $url, $protocols ) != $url )
1174 return apply_filters('clean_url', $url, $original_url, $context);
1177 function sanitize_url( $url, $protocols = null ) {
1178 return clean_url( $url, $protocols, 'db');
1181 // Borrowed from the PHP Manual user notes. Convert entities, while
1182 // preserving already-encoded entities:
1183 function htmlentities2($myHTML) {
1184 $translation_table=get_html_translation_table (HTML_ENTITIES,ENT_QUOTES);
1185 $translation_table[chr(38)] = '&';
1186 return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/","&" , strtr($myHTML, $translation_table));
1189 // Escape single quotes, specialchar double quotes, and fix line endings.
1190 function js_escape($text) {
1191 $safe_text = wp_specialchars($text, 'double');
1192 $safe_text = preg_replace('/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes($safe_text));
1193 $safe_text = preg_replace("/\r?\n/", "\\n", addslashes($safe_text));
1194 return apply_filters('js_escape', $safe_text, $text);
1197 // Escaping for HTML attributes
1198 function attribute_escape($text) {
1199 $safe_text = wp_specialchars($text, true);
1200 return apply_filters('attribute_escape', $safe_text, $text);
1203 // Escape a HTML tag name
1204 function tag_escape($tag_name) {
1205 $safe_tag = strtolower( preg_replace('[^a-zA-Z_:]', '', $tag_name) );
1206 return apply_filters('tag_escape', $safe_tag, $tag_name);
1210 * Escapes text for SQL LIKE special characters % and _
1212 * @param string text the text to be escaped
1213 * @return string text, safe for inclusion in LIKE query
1215 function like_escape($text) {
1216 return str_replace(array("%", "_"), array("\\%", "\\_"), $text);
1219 function wp_make_link_relative( $link ) {
1220 return preg_replace('|https?://[^/]+(/.*)|i', '$1', $link );
1223 function sanitize_option($option, $value) { // Remember to call stripslashes!
1227 $value = sanitize_email($value);
1230 case 'default_post_edit_rows':
1231 case 'mailserver_port':
1232 case 'comment_max_links':
1233 case 'page_on_front':
1234 case 'rss_excerpt_length':
1235 case 'default_category':
1236 case 'default_email_category':
1237 case 'default_link_category':
1238 $value = abs((int) $value);
1241 case 'posts_per_page':
1242 case 'posts_per_rss':
1243 $value = (int) $value;
1244 if ( empty($value) ) $value = 1;
1245 if ( $value < -1 ) $value = abs($value);
1248 case 'default_ping_status':
1249 case 'default_comment_status':
1250 // Options that if not there have 0 value but need to be something like "closed"
1251 if ( $value == '0' || $value == '')
1255 case 'blogdescription':
1257 $value = addslashes($value);
1258 $value = wp_filter_post_kses( $value ); // calls stripslashes then addslashes
1259 $value = stripslashes($value);
1260 $value = wp_specialchars( $value );
1263 case 'blog_charset':
1264 $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value); // strips slashes
1269 case 'mailserver_url':
1270 case 'mailserver_login':
1271 case 'mailserver_pass':
1274 $value = strip_tags($value);
1275 $value = addslashes($value);
1276 $value = wp_filter_kses($value); // calls stripslashes then addslashes
1277 $value = stripslashes($value);
1281 $value = preg_replace('/[^0-9:.-]/', '', $value); // strips slashes
1286 $value = stripslashes($value);
1287 $value = clean_url($value);
1290 $value = apply_filters("sanitize_option_{$option}", $value, $option);
1297 function wp_parse_str( $string, &$array ) {
1298 parse_str( $string, $array );
1299 if ( get_magic_quotes_gpc() )
1300 $array = stripslashes_deep( $array ); // parse_str() adds slashes if magicquotes is on. See: http://php.net/parse_str
1301 $array = apply_filters( 'wp_parse_str', $array );
1304 // Convert lone less than signs. KSES already converts lone greater than signs.
1305 function wp_pre_kses_less_than( $text ) {
1306 return preg_replace_callback('%<[^>]*?((?=<)|>|$)%', 'wp_pre_kses_less_than_callback', $text);
1309 function wp_pre_kses_less_than_callback( $matches ) {
1310 if ( false === strpos($matches[0], '>') )
1311 return wp_specialchars($matches[0]);
1316 * wp_sprintf() - sprintf() with filters
1318 function wp_sprintf( $pattern ) {
1319 $args = func_get_args( );
1320 $len = strlen($pattern);
1324 while ( $len > $start ) {
1325 // Last character: append and break
1326 if ( strlen($pattern) - 1 == $start ) {
1327 $result .= substr($pattern, -1);
1331 // Literal %: append and continue
1332 if ( substr($pattern, $start, 2) == '%%' ) {
1338 // Get fragment before next %
1339 $end = strpos($pattern, '%', $start + 1);
1340 if ( false === $end )
1342 $fragment = substr($pattern, $start, $end - $start);
1344 // Fragment has a specifier
1345 if ( $pattern{$start} == '%' ) {
1346 // Find numbered arguments or take the next one in order
1347 if ( preg_match('/^%(\d+)\$/', $fragment, $matches) ) {
1348 $arg = isset($args[$matches[1]]) ? $args[$matches[1]] : '';
1349 $fragment = str_replace("%{$matches[1]}$", '%', $fragment);
1352 $arg = isset($args[$arg_index]) ? $args[$arg_index] : '';
1355 // Apply filters OR sprintf
1356 $_fragment = apply_filters( 'wp_sprintf', $fragment, $arg );
1357 if ( $_fragment != $fragment )
1358 $fragment = $_fragment;
1360 $fragment = sprintf($fragment, strval($arg) );
1363 // Append to result and move to next fragment
1364 $result .= $fragment;
1371 * wp_sprintf_l - List specifier %l for wp_sprintf
1373 * @param unknown_type $pattern
1374 * @param unknown_type $args
1377 function wp_sprintf_l($pattern, $args) {
1379 if ( substr($pattern, 0, 2) != '%l' )
1382 // Nothing to work with
1386 // Translate and filter the delimiter set (avoid ampersands and entities here)
1387 $l = apply_filters('wp_sprintf_l', array(
1388 'between' => _c(', |between list items'),
1389 'between_last_two' => _c(', and |between last two list items'),
1390 'between_only_two' => _c(' and |between only two list items'),
1393 $args = (array) $args;
1394 $result = array_shift($args);
1395 if ( count($args) == 1 )
1396 $result .= $l['between_only_two'] . array_shift($args);
1397 // Loop when more than two args
1398 while ( count($args) ) {
1399 $arg = array_shift($args);
1401 $result .= $l['between_last_two'] . $arg;
1403 $result .= $l['between'] . $arg;
1405 return $result . substr($pattern, 2);
1409 * Safely extracts not more than the first $count characters from html string
1411 * UTF-8, tags and entities safe prefix extraction. Entities inside will *NOT* be
1412 * counted as one character. For example & will be counted as 4, < as 3, etc.
1414 * @param integer $str String to get the excerpt from
1415 * @param integer $count Maximum number of characters to take
1416 * @eaturn string the excerpt
1418 function wp_html_excerpt( $str, $count ) {
1419 $str = strip_tags( $str );
1420 $str = mb_strcut( $str, 0, $count );
1421 // remove part of an entity at the end
1422 $str = preg_replace( '/&[^;\s]{0,6}$/', '', $str );