Make WordPress Core

Ticket #9994: formatting.2.php

File formatting.2.php, 81.3 KB (added by kamiyeye, 17 years ago)

from 2.8 beta2

Line 
1<?php
2/**
3 * Main Wordpress Formatting API.
4 *
5 * Handles many functions for formatting output.
6 *
7 * @package WordPress
8 **/
9
10/**
11 * Replaces common plain text characters into formatted entities
12 *
13 * As an example,
14 * <code>
15 * 'cause today's effort makes it worth tomorrow's "holiday"...
16 * </code>
17 * Becomes:
18 * <code>
19 * &#8217;cause today&#8217;s effort makes it worth tomorrow&#8217;s &#8220;holiday&#8221;&#8230;
20 * </code>
21 * Code within certain html blocks are skipped.
22 *
23 * @since 0.71
24 * @uses $wp_cockneyreplace Array of formatted entities for certain common phrases
25 *
26 * @param string $text The text to be formatted
27 * @return string The string replaced with html entities
28 */
29function wptexturize($text) {
30 global $wp_cockneyreplace;
31 $output = '';
32 $curl = '';
33 $textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
34 $stop = count($textarr);
35 $no_texturize_tags = apply_filters('no_texturize_tags', array('pre', 'code', 'kbd', 'style', 'script', 'tt'));
36 $no_texturize_shortcodes = apply_filters('no_texturize_shortcodes', array('code'));
37 $no_texturize_tags_stack = array();
38 $no_texturize_shortcodes_stack = array();
39
40 // if a plugin has provided an autocorrect array, use it
41 if ( isset($wp_cockneyreplace) ) {
42 $cockney = array_keys($wp_cockneyreplace);
43 $cockneyreplace = array_values($wp_cockneyreplace);
44 } else {
45 $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");
46 $cockneyreplace = array("&#8217;tain&#8217;t","&#8217;twere","&#8217;twas","&#8217;tis","&#8217;twill","&#8217;til","&#8217;bout","&#8217;nuff","&#8217;round","&#8217;cause");
47 }
48
49 $static_characters = array_merge(array('---', ' -- ', '--', ' - ', 'xn&#8211;', '...', '``', '\'s', '\'\'', ' (tm)'), $cockney);
50 $static_replacements = array_merge(array('&#8212;', ' &#8212; ', '&#8211;', ' &#8211; ', 'xn--', '&#8230;', '&#8220;', '&#8217;s', '&#8221;', ' &#8482;'), $cockneyreplace);
51
52 $dynamic_characters = array('/\'(\d\d(?:&#8217;|\')?s)/', '/(\s|\A|")\'/', '/(\d+)"/', '/(\d+)\'/', '/(\S)\'([^\'\s])/', '/(\s|\A)"(?!\s)/', '/"(\s|\S|\Z)/', '/\'([\s.]|\Z)/', '/(\d+)x(\d+)/');
53 $dynamic_replacements = array('&#8217;$1','$1&#8216;', '$1&#8243;', '$1&#8242;', '$1&#8217;$2', '$1&#8220;$2', '&#8221;$1', '&#8217;$1', '$1&#215;$2');
54
55 for ( $i = 0; $i < $stop; $i++ ) {
56 $curl = $textarr[$i];
57
58 if ( !empty($curl) && '<' != $curl{0} && '[' != $curl{0}
59 && empty($no_texturize_shortcodes_stack) && empty($no_texturize_tags_stack)) { // If it's not a tag
60 // static strings
61 $curl = str_replace($static_characters, $static_replacements, $curl);
62 // regular expressions
63 $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
64 } else {
65 wptexturize_pushpop_element($curl, $no_texturize_tags_stack, $no_texturize_tags, '<', '>');
66 wptexturize_pushpop_element($curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes, '[', ']');
67 }
68
69 $curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&#038;$1', $curl);
70 $output .= $curl;
71 }
72
73 return $output;
74}
75
76function wptexturize_pushpop_element($text, &$stack, $disabled_elements, $opening = '<', $closing = '>') {
77 $o = preg_quote($opening);
78 $c = preg_quote($closing);
79 foreach($disabled_elements as $element) {
80 if (preg_match('/^'.$o.$element.'\b/', $text)) array_push($stack, $element);
81 if (preg_match('/^'.$o.'\/'.$element.$c.'/', $text)) {
82 $last = array_pop($stack);
83 // disable texturize until we find a closing tag of our type (e.g. <pre>)
84 // even if there was invalid nesting before that
85 // Example: in the case <pre>sadsadasd</code>"baba"</pre> "baba" won't be texturized
86 if ($last != $element) array_push($stack, $last);
87 }
88 }
89}
90
91/**
92 * Accepts matches array from preg_replace_callback in wpautop() or a string.
93 *
94 * Ensures that the contents of a <<pre>>...<</pre>> HTML block are not
95 * converted into paragraphs or line-breaks.
96 *
97 * @since 1.2.0
98 *
99 * @param array|string $matches The array or string
100 * @return string The pre block without paragraph/line-break conversion.
101 */
102function clean_pre($matches) {
103 if ( is_array($matches) )
104 $text = $matches[1] . $matches[2] . "</pre>";
105 else
106 $text = $matches;
107
108 $text = str_replace('<br />', '', $text);
109 $text = str_replace('<p>', "\n", $text);
110 $text = str_replace('</p>', '', $text);
111
112 return $text;
113}
114
115/**
116 * Replaces double line-breaks with paragraph elements.
117 *
118 * A group of regex replaces used to identify text formatted with newlines and
119 * replace double line-breaks with HTML paragraph tags. The remaining
120 * line-breaks after conversion become <<br />> tags, unless $br is set to '0'
121 * or 'false'.
122 *
123 * @since 0.71
124 *
125 * @param string $pee The text which has to be formatted.
126 * @param int|bool $br Optional. If set, this will convert all remaining line-breaks after paragraphing. Default true.
127 * @return string Text which has been converted into correct paragraph tags.
128 */
129function wpautop($pee, $br = 1) {
130 if ( trim($pee) === '' )
131 return '';
132 $pee = $pee . "\n"; // just to make things a little easier, pad the end
133 $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
134 // Space things out a little
135 $allblocks = '(?:table|thead|tfoot|caption|col|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)';
136 $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
137 $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
138 $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
139 if ( strpos($pee, '<object') !== false ) {
140 $pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed
141 $pee = preg_replace('|\s*</embed>\s*|', '</embed>', $pee);
142 }
143 $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
144 // make paragraphs, including one at the end
145 $pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY);
146 $pee = '';
147 foreach ( $pees as $tinkle )
148 $pee .= '<p>' . trim($tinkle, "\n") . "</p>\n";
149 $pee = preg_replace('|<p>\s*</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
150 $pee = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $pee);
151 $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
152 $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
153 $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
154 $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
155 $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
156 $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
157 if ($br) {
158 $pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', create_function('$matches', 'return str_replace("\n", "<WPPreserveNewline />", $matches[0]);'), $pee);
159 $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
160 $pee = str_replace('<WPPreserveNewline />', "\n", $pee);
161 }
162 $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
163 $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
164 if (strpos($pee, '<pre') !== false)
165 $pee = preg_replace_callback('!(<pre[^>]*>)(.*?)</pre>!is', 'clean_pre', $pee );
166 $pee = preg_replace( "|\n</p>$|", '</p>', $pee );
167 $pee = preg_replace('/<p>\s*?(' . get_shortcode_regex() . ')\s*<\/p>/s', '$1', $pee); // don't auto-p wrap shortcodes that stand alone
168
169 return $pee;
170}
171
172/**
173 * Checks to see if a string is utf8 encoded.
174 *
175 * NOTE: This function checks for 5-Byte sequences, UTF8
176 * has Bytes Sequences with a maximum length of 4.
177 *
178 * @author bmorel at ssi dot fr (modified)
179 * @since 1.2.1
180 *
181 * @param string $str The string to be checked
182 * @return bool True if $str fits a UTF-8 model, false otherwise.
183 */
184function seems_utf8(&$str) {
185 $length = strlen($str);
186 for ($i=0; $i < $length; $i++) {
187 $c = ord($str[$i]);
188 if ($c < 0x80) $n = 0; # 0bbbbbbb
189 elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb
190 elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb
191 elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb
192 elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb
193 elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b
194 else return false; # Does not match any model
195 for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
196 if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))
197 return false;
198 }
199 }
200 return true;
201}
202
203/**
204 * Converts a number of special characters into their HTML entities.
205 *
206 * Specifically deals with: &, <, >, ", and '.
207 *
208 * $quote_style can be set to ENT_COMPAT to encode " to
209 * &quot;, or ENT_QUOTES to do both. Default is ENT_NOQUOTES where no quotes are encoded.
210 *
211 * @since 1.2.2
212 *
213 * @param string $string The text which is to be encoded.
214 * @param mixed $quote_style Optional. Converts double quotes if set to ENT_COMPAT, both single and double if set to ENT_QUOTES or none if set to ENT_NOQUOTES. Also compatible with old values; converting single quotes if set to 'single', double if set to 'double' or both if otherwise set. Default is ENT_NOQUOTES.
215 * @param string $charset Optional. The character encoding of the string. Default is false.
216 * @param boolean $double_encode Optional. Whether or not to encode existing html entities. Default is false.
217 * @return string The encoded text with HTML entities.
218 */
219function _wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
220 $string = (string) $string;
221
222 if ( 0 === strlen( $string ) ) {
223 return '';
224 }
225
226 // Don't bother if there are no specialchars - saves some processing
227 if ( !preg_match( '/[&<>"\']/', $string ) ) {
228 return $string;
229 }
230
231 // Account for the previous behaviour of the function when the $quote_style is not an accepted value
232 if ( empty( $quote_style ) ) {
233 $quote_style = ENT_NOQUOTES;
234 } elseif ( !in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) {
235 $quote_style = ENT_QUOTES;
236 }
237
238 // Store the site charset as a static to avoid multiple calls to wp_load_alloptions()
239 if ( !$charset ) {
240 static $_charset;
241 if ( !isset( $_charset ) ) {
242 $alloptions = wp_load_alloptions();
243 $_charset = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : '';
244 }
245 $charset = $_charset;
246 }
247 if ( in_array( $charset, array( 'utf8', 'utf-8', 'UTF8' ) ) ) {
248 $charset = 'UTF-8';
249 }
250
251 $_quote_style = $quote_style;
252
253 if ( $quote_style === 'double' ) {
254 $quote_style = ENT_COMPAT;
255 $_quote_style = ENT_COMPAT;
256 } elseif ( $quote_style === 'single' ) {
257 $quote_style = ENT_NOQUOTES;
258 }
259
260 // Handle double encoding ourselves
261 if ( !$double_encode ) {
262 $string = wp_specialchars_decode( $string, $_quote_style );
263 $string = preg_replace( '/&(#?x?[0-9a-z]+);/i', '|wp_entity|$1|/wp_entity|', $string );
264 }
265
266 $string = @htmlspecialchars( $string, $quote_style, $charset );
267
268 // Handle double encoding ourselves
269 if ( !$double_encode ) {
270 $string = str_replace( array( '|wp_entity|', '|/wp_entity|' ), array( '&', ';' ), $string );
271 }
272
273 // Backwards compatibility
274 if ( 'single' === $_quote_style ) {
275 $string = str_replace( "'", '&#039;', $string );
276 }
277
278 return $string;
279}
280
281/**
282 * Converts a number of HTML entities into their special characters.
283 *
284 * Specifically deals with: &, <, >, ", and '.
285 *
286 * $quote_style can be set to ENT_COMPAT to decode " entities,
287 * or ENT_QUOTES to do both " and '. Default is ENT_NOQUOTES where no quotes are decoded.
288 *
289 * @since 2.8
290 *
291 * @param string $string The text which is to be decoded.
292 * @param mixed $quote_style Optional. Converts double quotes if set to ENT_COMPAT, both single and double if set to ENT_QUOTES or none if set to ENT_NOQUOTES. Also compatible with old _wp_specialchars() values; converting single quotes if set to 'single', double if set to 'double' or both if otherwise set. Default is ENT_NOQUOTES.
293 * @return string The decoded text without HTML entities.
294 */
295function wp_specialchars_decode( $string, $quote_style = ENT_NOQUOTES ) {
296 $string = (string) $string;
297
298 if ( 0 === strlen( $string ) ) {
299 return '';
300 }
301
302 // Don't bother if there are no entities - saves a lot of processing
303 if ( strpos( $string, '&' ) === false ) {
304 return $string;
305 }
306
307 // Match the previous behaviour of _wp_specialchars() when the $quote_style is not an accepted value
308 if ( empty( $quote_style ) ) {
309 $quote_style = ENT_NOQUOTES;
310 } elseif ( !in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) {
311 $quote_style = ENT_QUOTES;
312 }
313
314 // More complete than get_html_translation_table( HTML_SPECIALCHARS )
315 $single = array( '&#039;' => '\'', '&#x27;' => '\'' );
316 $single_preg = array( '/&#0*39;/' => '&#039;', '/&#x0*27;/i' => '&#x27;' );
317 $double = array( '&quot;' => '"', '&#034;' => '"', '&#x22;' => '"' );
318 $double_preg = array( '/&#0*34;/' => '&#034;', '/&#x0*22;/i' => '&#x22;' );
319 $others = array( '&lt;' => '<', '&#060;' => '<', '&gt;' => '>', '&#062;' => '>', '&amp;' => '&', '&#038;' => '&', '&#x26;' => '&' );
320 $others_preg = array( '/&#0*60;/' => '&#060;', '/&#0*62;/' => '&#062;', '/&#0*38;/' => '&#038;', '/&#x0*26;/i' => '&#x26;' );
321
322 if ( $quote_style === ENT_QUOTES ) {
323 $translation = array_merge( $single, $double, $others );
324 $translation_preg = array_merge( $single_preg, $double_preg, $others_preg );
325 } elseif ( $quote_style === ENT_COMPAT || $quote_style === 'double' ) {
326 $translation = array_merge( $double, $others );
327 $translation_preg = array_merge( $double_preg, $others_preg );
328 } elseif ( $quote_style === 'single' ) {
329 $translation = array_merge( $single, $others );
330 $translation_preg = array_merge( $single_preg, $others_preg );
331 } elseif ( $quote_style === ENT_NOQUOTES ) {
332 $translation = $others;
333 $translation_preg = $others_preg;
334 }
335
336 // Remove zero padding on numeric entities
337 $string = preg_replace( array_keys( $translation_preg ), array_values( $translation_preg ), $string );
338
339 // Replace characters according to translation table
340 return strtr( $string, $translation );
341}
342
343/**
344 * Checks for invalid UTF8 in a string.
345 *
346 * @since 2.8
347 *
348 * @param string $string The text which is to be checked.
349 * @param boolean $strip Optional. Whether to attempt to strip out invalid UTF8. Default is false.
350 * @return string The checked text.
351 */
352function wp_check_invalid_utf8( $string, $strip = false ) {
353 $string = (string) $string;
354
355 if ( 0 === strlen( $string ) ) {
356 return '';
357 }
358
359 // Store the site charset as a static to avoid multiple calls to get_option()
360 static $is_utf8;
361 if ( !isset( $is_utf8 ) ) {
362 $is_utf8 = in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) );
363 }
364 if ( !$is_utf8 ) {
365 return $string;
366 }
367
368 // Check for support for utf8 in the installed PCRE library once and store the result in a static
369 static $utf8_pcre;
370 if ( !isset( $utf8_pcre ) ) {
371 $utf8_pcre = @preg_match( '/^./u', 'a' );
372 }
373 // We can't demand utf8 in the PCRE installation, so just return the string in those cases
374 if ( !$utf8_pcre ) {
375 return $string;
376 }
377
378 // preg_match fails when it encounters invalid UTF8 in $string
379 if ( 1 === @preg_match( '/^./us', $string ) ) {
380 return $string;
381 }
382
383 // Attempt to strip the bad chars if requested (not recommended)
384 if ( $strip && function_exists( 'iconv' ) ) {
385 return iconv( 'utf-8', 'utf-8', $string );
386 }
387
388 return '';
389}
390
391/**
392 * Encode the Unicode values to be used in the URI.
393 *
394 * @since 1.5.0
395 *
396 * @param string $utf8_string
397 * @param int $length Max length of the string
398 * @return string String with Unicode encoded for URI.
399 */
400function utf8_uri_encode( $utf8_string, $length = 0 ) {
401 $unicode = '';
402 $values = array();
403 $num_octets = 1;
404 $unicode_length = 0;
405
406 $string_length = strlen( $utf8_string );
407 for ($i = 0; $i < $string_length; $i++ ) {
408
409 $value = ord( $utf8_string[ $i ] );
410
411 if ( $value < 128 ) {
412 if ( $length && ( $unicode_length >= $length ) )
413 break;
414 $unicode .= chr($value);
415 $unicode_length++;
416 } else {
417 if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3;
418
419 $values[] = $value;
420
421 if ( $length && ( $unicode_length + ($num_octets * 3) ) > $length )
422 break;
423 if ( count( $values ) == $num_octets ) {
424 if ($num_octets == 3) {
425 $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]);
426 $unicode_length += 9;
427 } else {
428 $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]);
429 $unicode_length += 6;
430 }
431
432 $values = array();
433 $num_octets = 1;
434 }
435 }
436 }
437
438 return $unicode;
439}
440
441/**
442 * Converts all accent characters to ASCII characters.
443 *
444 * If there are no accent characters, then the string given is just returned.
445 *
446 * @since 1.2.1
447 *
448 * @param string $string Text that might have accent characters
449 * @return string Filtered string with replaced "nice" characters.
450 */
451function remove_accents($string) {
452 if ( !preg_match('/[\x80-\xff]/', $string) )
453 return $string;
454
455 if (seems_utf8($string)) {
456 $chars = array(
457 // Decompositions for Latin-1 Supplement
458 chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
459 chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
460 chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
461 chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
462 chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
463 chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
464 chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
465 chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
466 chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
467 chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
468 chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
469 chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
470 chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
471 chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
472 chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
473 chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
474 chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
475 chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
476 chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
477 chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
478 chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
479 chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
480 chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
481 chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
482 chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
483 chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
484 chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
485 chr(195).chr(191) => 'y',
486 // Decompositions for Latin Extended-A
487 chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
488 chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
489 chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
490 chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
491 chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
492 chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
493 chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
494 chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
495 chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
496 chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
497 chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
498 chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
499 chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
500 chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
501 chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
502 chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
503 chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
504 chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
505 chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
506 chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
507 chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
508 chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
509 chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
510 chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
511 chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
512 chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
513 chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
514 chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
515 chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
516 chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
517 chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
518 chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
519 chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
520 chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
521 chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
522 chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
523 chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
524 chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
525 chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
526 chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
527 chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
528 chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
529 chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
530 chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
531 chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
532 chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
533 chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
534 chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
535 chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
536 chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
537 chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
538 chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
539 chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
540 chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
541 chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
542 chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
543 chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
544 chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
545 chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
546 chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
547 chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
548 chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
549 chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
550 chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
551 // Euro Sign
552 chr(226).chr(130).chr(172) => 'E',
553 // GBP (Pound) Sign
554 chr(194).chr(163) => '');
555
556 $string = strtr($string, $chars);
557 } else {
558 // Assume ISO-8859-1 if not UTF-8
559 $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
560 .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
561 .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
562 .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
563 .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
564 .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
565 .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
566 .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
567 .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
568 .chr(252).chr(253).chr(255);
569
570 $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
571
572 $string = strtr($string, $chars['in'], $chars['out']);
573 $double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
574 $double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
575 $string = str_replace($double_chars['in'], $double_chars['out'], $string);
576 }
577
578 return $string;
579}
580
581/**
582 * Sanitizes a filename replacing whitespace with dashes
583 *
584 * Removes special characters that are illegal in filenames on certain
585 * operating systems and special characters requiring special escaping
586 * to manipulate at the command line. Replaces spaces and consecutive
587 * dashes with a single dash. Trim period, dash and underscore from beginning
588 * and end of filename.
589 *
590 * @since 2.1.0
591 *
592 * @param string $filename The filename to be sanitized
593 * @return string The sanitized filename
594 */
595function sanitize_file_name( $filename ) {
596 $filename_raw = $filename;
597 $special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}");
598 $special_chars = apply_filters('sanitize_file_name_chars', $special_chars, $filename_raw);
599 $filename = str_replace($special_chars, '', $filename);
600 $filename = preg_replace('/[\s-]+/', '-', $filename);
601 $filename = trim($filename, '.-_');
602 return apply_filters('sanitize_file_name', $filename, $filename_raw);
603}
604
605/**
606 * Sanitize username stripping out unsafe characters.
607 *
608 * If $strict is true, only alphanumeric characters (as well as _, space, ., -,
609 * @) are returned.
610 * Removes tags, octets, entities, and if strict is enabled, will remove all
611 * non-ASCII characters. After sanitizing, it passes the username, raw username
612 * (the username in the parameter), and the strict parameter as parameters for
613 * the filter.
614 *
615 * @since 2.0.0
616 * @uses apply_filters() Calls 'sanitize_user' hook on username, raw username,
617 * and $strict parameter.
618 *
619 * @param string $username The username to be sanitized.
620 * @param bool $strict If set limits $username to specific characters. Default false.
621 * @return string The sanitized username, after passing through filters.
622 */
623function sanitize_user( $username, $strict = false ) {
624 $raw_username = $username;
625 $username = strip_tags($username);
626 // Kill octets
627 $username = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '', $username);
628 $username = preg_replace('/&.+?;/', '', $username); // Kill entities
629
630 // If strict, reduce to ASCII for max portability.
631 if ( $strict )
632 $username = preg_replace('|[^a-z0-9 _.\-@]|i', '', $username);
633
634 // Consolidate contiguous whitespace
635 $username = preg_replace('|\s+|', ' ', $username);
636
637 return apply_filters('sanitize_user', $username, $raw_username, $strict);
638}
639
640/**
641 * Sanitizes title or use fallback title.
642 *
643 * Specifically, HTML and PHP tags are stripped. Further actions can be added
644 * via the plugin API. If $title is empty and $fallback_title is set, the latter
645 * will be used.
646 *
647 * @since 1.0.0
648 *
649 * @param string $title The string to be sanitized.
650 * @param string $fallback_title Optional. A title to use if $title is empty.
651 * @return string The sanitized string.
652 */
653function sanitize_title($title, $fallback_title = '') {
654 $raw_title = $title;
655 $title = strip_tags($title);
656 $title = apply_filters('sanitize_title', $title, $raw_title);
657
658 if ( '' === $title || false === $title )
659 $title = $fallback_title;
660
661 return $title;
662}
663
664/**
665 * Sanitizes title, replacing whitespace with dashes.
666 *
667 * Limits the output to alphanumeric characters, underscore (_) and dash (-).
668 * Whitespace becomes a dash.
669 *
670 * @since 1.2.0
671 *
672 * @param string $title The title to be sanitized.
673 * @return string The sanitized title.
674 */
675function sanitize_title_with_dashes($title) {
676 $title = strip_tags($title);
677 // Preserve escaped octets.
678 $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
679 // Remove percent signs that are not part of an octet.
680 $title = str_replace('%', '', $title);
681 // Restore octets.
682 $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
683
684 $title = remove_accents($title);
685 if (seems_utf8($title)) {
686 if (function_exists('mb_strtolower')) {
687 $title = mb_strtolower($title, 'UTF-8');
688 }
689 $title = utf8_uri_encode($title, 200);
690 }
691
692 $title = strtolower($title);
693 $title = preg_replace('/&.+?;/', '', $title); // kill entities
694 $title = str_replace('.', '-', $title);
695 $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
696 $title = preg_replace('/\s+/', '-', $title);
697 $title = preg_replace('|-+|', '-', $title);
698 $title = trim($title, '-');
699
700 return $title;
701}
702
703/**
704 * Ensures a string is a valid SQL order by clause.
705 *
706 * Accepts one or more columns, with or without ASC/DESC, and also accepts
707 * RAND().
708 *
709 * @since 2.5.1
710 *
711 * @param string $orderby Order by string to be checked.
712 * @return string|false Returns the order by clause if it is a match, false otherwise.
713 */
714function sanitize_sql_orderby( $orderby ){
715 preg_match('/^\s*([a-z0-9_]+(\s+(ASC|DESC))?(\s*,\s*|\s*$))+|^\s*RAND\(\s*\)\s*$/i', $orderby, $obmatches);
716 if ( !$obmatches )
717 return false;
718 return $orderby;
719}
720
721/**
722 * Santizes a html classname to ensure it only contains valid characters
723 *
724 * Strips the string down to A-Z,a-z,0-9,'-' if this results in an empty
725 * string then it will return the alternative value supplied.
726 *
727 * @todo Expand to support the full range of CDATA that a class attribute can contain.
728 *
729 * @since 2.8.0
730 *
731 * @param string $class The classname to be sanitized
732 * @param string $fallback The value to return if the sanitization end's up as an empty string.
733 * @return string The sanitized value
734 */
735function sanitize_html_class($class, $fallback){
736 //Strip out any % encoded octets
737 $sanitized = preg_replace('|%[a-fA-F0-9][a-fA-F0-9]|', '', $class);
738
739 //Limit to A-Z,a-z,0-9,'-'
740 $sanitized = preg_replace('/[^A-Za-z0-9-]/', '', $sanitized);
741
742 if ('' == $sanitized)
743 $sanitized = $fallback;
744
745 return apply_filters('sanitize_html_class',$sanitized, $class, $fallback);
746}
747
748/**
749 * Converts a number of characters from a string.
750 *
751 * Metadata tags <<title>> and <<category>> are removed, <<br>> and <<hr>> are
752 * converted into correct XHTML and Unicode characters are converted to the
753 * valid range.
754 *
755 * @since 0.71
756 *
757 * @param string $content String of characters to be converted.
758 * @param string $deprecated Not used.
759 * @return string Converted string.
760 */
761function convert_chars($content, $deprecated = '') {
762 // Translation of invalid Unicode references range to valid range
763 $wp_htmltranswinuni = array(
764 '&#128;' => '&#8364;', // the Euro sign
765 '&#129;' => '',
766 '&#130;' => '&#8218;', // these are Windows CP1252 specific characters
767 '&#131;' => '&#402;', // they would look weird on non-Windows browsers
768 '&#132;' => '&#8222;',
769 '&#133;' => '&#8230;',
770 '&#134;' => '&#8224;',
771 '&#135;' => '&#8225;',
772 '&#136;' => '&#710;',
773 '&#137;' => '&#8240;',
774 '&#138;' => '&#352;',
775 '&#139;' => '&#8249;',
776 '&#140;' => '&#338;',
777 '&#141;' => '',
778 '&#142;' => '&#382;',
779 '&#143;' => '',
780 '&#144;' => '',
781 '&#145;' => '&#8216;',
782 '&#146;' => '&#8217;',
783 '&#147;' => '&#8220;',
784 '&#148;' => '&#8221;',
785 '&#149;' => '&#8226;',
786 '&#150;' => '&#8211;',
787 '&#151;' => '&#8212;',
788 '&#152;' => '&#732;',
789 '&#153;' => '&#8482;',
790 '&#154;' => '&#353;',
791 '&#155;' => '&#8250;',
792 '&#156;' => '&#339;',
793 '&#157;' => '',
794 '&#158;' => '',
795 '&#159;' => '&#376;'
796 );
797
798 // Remove metadata tags
799 $content = preg_replace('/<title>(.+?)<\/title>/','',$content);
800 $content = preg_replace('/<category>(.+?)<\/category>/','',$content);
801
802 // Converts lone & characters into &#38; (a.k.a. &amp;)
803 $content = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/i', '&#038;$1', $content);
804
805 // Fix Word pasting
806 $content = strtr($content, $wp_htmltranswinuni);
807
808 // Just a little XHTML help
809 $content = str_replace('<br>', '<br />', $content);
810 $content = str_replace('<hr>', '<hr />', $content);
811
812 return $content;
813}
814
815/**
816 * Callback used to change %uXXXX to &#YYY; syntax
817 *
818 * @since 2.8?
819 *
820 * @param array $matches Single Match
821 * @return string An HTML entity
822 */
823function funky_javascript_callback($matches) {
824 return "&#".base_convert($matches[1],16,10).";";
825}
826
827/**
828 * Fixes javascript bugs in browsers.
829 *
830 * Converts unicode characters to HTML numbered entities.
831 *
832 * @since 1.5.0
833 * @uses $is_macIE
834 * @uses $is_winIE
835 *
836 * @param string $text Text to be made safe.
837 * @return string Fixed text.
838 */
839function funky_javascript_fix($text) {
840 // Fixes for browsers' javascript bugs
841 global $is_macIE, $is_winIE;
842
843 if ( $is_winIE || $is_macIE )
844 $text = preg_replace_callback("/\%u([0-9A-F]{4,4})/",
845 "funky_javascript_callback",
846 $text);
847
848 return $text;
849}
850
851/**
852 * Will only balance the tags if forced to and the option is set to balance tags.
853 *
854 * The option 'use_balanceTags' is used for whether the tags will be balanced.
855 * Both the $force parameter and 'use_balanceTags' option will have to be true
856 * before the tags will be balanced.
857 *
858 * @since 0.71
859 *
860 * @param string $text Text to be balanced
861 * @param bool $force Forces balancing, ignoring the value of the option. Default false.
862 * @return string Balanced text
863 */
864function balanceTags( $text, $force = false ) {
865 if ( !$force && get_option('use_balanceTags') == 0 )
866 return $text;
867 return force_balance_tags( $text );
868}
869
870/**
871 * Balances tags of string using a modified stack.
872 *
873 * @since 2.0.4
874 *
875 * @author Leonard Lin <leonard@acm.org>
876 * @license GPL v2.0
877 * @copyright November 4, 2001
878 * @version 1.1
879 * @todo Make better - change loop condition to $text in 1.2
880 * @internal Modified by Scott Reilly (coffee2code) 02 Aug 2004
881 * 1.1 Fixed handling of append/stack pop order of end text
882 * Added Cleaning Hooks
883 * 1.0 First Version
884 *
885 * @param string $text Text to be balanced.
886 * @return string Balanced text.
887 */
888function force_balance_tags( $text ) {
889 $tagstack = array(); $stacksize = 0; $tagqueue = ''; $newtext = '';
890 $single_tags = array('br', 'hr', 'img', 'input'); //Known single-entity/self-closing tags
891 $nestable_tags = array('blockquote', 'div', 'span'); //Tags that can be immediately nested within themselves
892
893 # WP bug fix for comments - in case you REALLY meant to type '< !--'
894 $text = str_replace('< !--', '< !--', $text);
895 # WP bug fix for LOVE <3 (and other situations with '<' before a number)
896 $text = preg_replace('#<([0-9]{1})#', '&lt;$1', $text);
897
898 while (preg_match("/<(\/?\w*)\s*([^>]*)>/",$text,$regex)) {
899 $newtext .= $tagqueue;
900
901 $i = strpos($text,$regex[0]);
902 $l = strlen($regex[0]);
903
904 // clear the shifter
905 $tagqueue = '';
906 // Pop or Push
907 if ( isset($regex[1][0]) && '/' == $regex[1][0] ) { // End Tag
908 $tag = strtolower(substr($regex[1],1));
909 // if too many closing tags
910 if($stacksize <= 0) {
911 $tag = '';
912 //or close to be safe $tag = '/' . $tag;
913 }
914 // if stacktop value = tag close value then pop
915 else if ($tagstack[$stacksize - 1] == $tag) { // found closing tag
916 $tag = '</' . $tag . '>'; // Close Tag
917 // Pop
918 array_pop ($tagstack);
919 $stacksize--;
920 } else { // closing tag not at top, search for it
921 for ($j=$stacksize-1;$j>=0;$j--) {
922 if ($tagstack[$j] == $tag) {
923 // add tag to tagqueue
924 for ($k=$stacksize-1;$k>=$j;$k--){
925 $tagqueue .= '</' . array_pop ($tagstack) . '>';
926 $stacksize--;
927 }
928 break;
929 }
930 }
931 $tag = '';
932 }
933 } else { // Begin Tag
934 $tag = strtolower($regex[1]);
935
936 // Tag Cleaning
937
938 // If self-closing or '', don't do anything.
939 if((substr($regex[2],-1) == '/') || ($tag == '')) {
940 }
941 // ElseIf it's a known single-entity tag but it doesn't close itself, do so
942 elseif ( in_array($tag, $single_tags) ) {
943 $regex[2] .= '/';
944 } else { // Push the tag onto the stack
945 // If the top of the stack is the same as the tag we want to push, close previous tag
946 if (($stacksize > 0) && !in_array($tag, $nestable_tags) && ($tagstack[$stacksize - 1] == $tag)) {
947 $tagqueue = '</' . array_pop ($tagstack) . '>';
948 $stacksize--;
949 }
950 $stacksize = array_push ($tagstack, $tag);
951 }
952
953 // Attributes
954 $attributes = $regex[2];
955 if($attributes) {
956 $attributes = ' '.$attributes;
957 }
958 $tag = '<'.$tag.$attributes.'>';
959 //If already queuing a close tag, then put this tag on, too
960 if ($tagqueue) {
961 $tagqueue .= $tag;
962 $tag = '';
963 }
964 }
965 $newtext .= substr($text,0,$i) . $tag;
966 $text = substr($text,$i+$l);
967 }
968
969 // Clear Tag Queue
970 $newtext .= $tagqueue;
971
972 // Add Remaining text
973 $newtext .= $text;
974
975 // Empty Stack
976 while($x = array_pop($tagstack)) {
977 $newtext .= '</' . $x . '>'; // Add remaining tags to close
978 }
979
980 // WP fix for the bug with HTML comments
981 $newtext = str_replace("< !--","<!--",$newtext);
982 $newtext = str_replace("< !--","< !--",$newtext);
983
984 return $newtext;
985}
986
987/**
988 * Acts on text which is about to be edited.
989 *
990 * Unless $richedit is set, it is simply a holder for the 'format_to_edit'
991 * filter. If $richedit is set true htmlspecialchars() will be run on the
992 * content, converting special characters to HTMl entities.
993 *
994 * @since 0.71
995 *
996 * @param string $content The text about to be edited.
997 * @param bool $richedit Whether or not the $content should pass through htmlspecialchars(). Default false.
998 * @return string The text after the filter (and possibly htmlspecialchars()) has been run.
999 */
1000function format_to_edit($content, $richedit = false) {
1001 $content = apply_filters('format_to_edit', $content);
1002 if (! $richedit )
1003 $content = htmlspecialchars($content);
1004 return $content;
1005}
1006
1007/**
1008 * Holder for the 'format_to_post' filter.
1009 *
1010 * @since 0.71
1011 *
1012 * @param string $content The text to pass through the filter.
1013 * @return string Text returned from the 'format_to_post' filter.
1014 */
1015function format_to_post($content) {
1016 $content = apply_filters('format_to_post', $content);
1017 return $content;
1018}
1019
1020/**
1021 * Add leading zeros when necessary.
1022 *
1023 * If you set the threshold to '4' and the number is '10', then you will get
1024 * back '0010'. If you set the number to '4' and the number is '5000', then you
1025 * will get back '5000'.
1026 *
1027 * Uses sprintf to append the amount of zeros based on the $threshold parameter
1028 * and the size of the number. If the number is large enough, then no zeros will
1029 * be appended.
1030 *
1031 * @since 0.71
1032 *
1033 * @param mixed $number Number to append zeros to if not greater than threshold.
1034 * @param int $threshold Digit places number needs to be to not have zeros added.
1035 * @return string Adds leading zeros to number if needed.
1036 */
1037function zeroise($number, $threshold) {
1038 return sprintf('%0'.$threshold.'s', $number);
1039}
1040
1041/**
1042 * Adds backslashes before letters and before a number at the start of a string.
1043 *
1044 * @since 0.71
1045 *
1046 * @param string $string Value to which backslashes will be added.
1047 * @return string String with backslashes inserted.
1048 */
1049function backslashit($string) {
1050 $string = preg_replace('/^([0-9])/', '\\\\\\\\\1', $string);
1051 $string = preg_replace('/([a-z])/i', '\\\\\1', $string);
1052 return $string;
1053}
1054
1055/**
1056 * Appends a trailing slash.
1057 *
1058 * Will remove trailing slash if it exists already before adding a trailing
1059 * slash. This prevents double slashing a string or path.
1060 *
1061 * The primary use of this is for paths and thus should be used for paths. It is
1062 * not restricted to paths and offers no specific path support.
1063 *
1064 * @since 1.2.0
1065 * @uses untrailingslashit() Unslashes string if it was slashed already.
1066 *
1067 * @param string $string What to add the trailing slash to.
1068 * @return string String with trailing slash added.
1069 */
1070function trailingslashit($string) {
1071 return untrailingslashit($string) . '/';
1072}
1073
1074/**
1075 * Removes trailing slash if it exists.
1076 *
1077 * The primary use of this is for paths and thus should be used for paths. It is
1078 * not restricted to paths and offers no specific path support.
1079 *
1080 * @since 2.2.0
1081 *
1082 * @param string $string What to remove the trailing slash from.
1083 * @return string String without the trailing slash.
1084 */
1085function untrailingslashit($string) {
1086 return rtrim($string, '/');
1087}
1088
1089/**
1090 * Adds slashes to escape strings.
1091 *
1092 * Slashes will first be removed if magic_quotes_gpc is set, see {@link
1093 * http://www.php.net/magic_quotes} for more details.
1094 *
1095 * @since 0.71
1096 *
1097 * @param string $gpc The string returned from HTTP request data.
1098 * @return string Returns a string escaped with slashes.
1099 */
1100function addslashes_gpc($gpc) {
1101 global $wpdb;
1102
1103 if (get_magic_quotes_gpc()) {
1104 $gpc = stripslashes($gpc);
1105 }
1106
1107 return $wpdb->escape($gpc);
1108}
1109
1110/**
1111 * Navigates through an array and removes slashes from the values.
1112 *
1113 * If an array is passed, the array_map() function causes a callback to pass the
1114 * value back to the function. The slashes from this value will removed.
1115 *
1116 * @since 2.0.0
1117 *
1118 * @param array|string $value The array or string to be striped.
1119 * @return array|string Stripped array (or string in the callback).
1120 */
1121function stripslashes_deep($value) {
1122 $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
1123 return $value;
1124}
1125
1126/**
1127 * Navigates through an array and encodes the values to be used in a URL.
1128 *
1129 * Uses a callback to pass the value of the array back to the function as a
1130 * string.
1131 *
1132 * @since 2.2.0
1133 *
1134 * @param array|string $value The array or string to be encoded.
1135 * @return array|string $value The encoded array (or string from the callback).
1136 */
1137function urlencode_deep($value) {
1138 $value = is_array($value) ? array_map('urlencode_deep', $value) : urlencode($value);
1139 return $value;
1140}
1141
1142/**
1143 * Converts email addresses characters to HTML entities to block spam bots.
1144 *
1145 * @since 0.71
1146 *
1147 * @param string $emailaddy Email address.
1148 * @param int $mailto Optional. Range from 0 to 1. Used for encoding.
1149 * @return string Converted email address.
1150 */
1151function antispambot($emailaddy, $mailto=0) {
1152 $emailNOSPAMaddy = '';
1153 srand ((float) microtime() * 1000000);
1154 for ($i = 0; $i < strlen($emailaddy); $i = $i + 1) {
1155 $j = floor(rand(0, 1+$mailto));
1156 if ($j==0) {
1157 $emailNOSPAMaddy .= '&#'.ord(substr($emailaddy,$i,1)).';';
1158 } elseif ($j==1) {
1159 $emailNOSPAMaddy .= substr($emailaddy,$i,1);
1160 } elseif ($j==2) {
1161 $emailNOSPAMaddy .= '%'.zeroise(dechex(ord(substr($emailaddy, $i, 1))), 2);
1162 }
1163 }
1164 $emailNOSPAMaddy = str_replace('@','&#64;',$emailNOSPAMaddy);
1165 return $emailNOSPAMaddy;
1166}
1167
1168/**
1169 * Callback to convert URI match to HTML A element.
1170 *
1171 * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
1172 * make_clickable()}.
1173 *
1174 * @since 2.3.2
1175 * @access private
1176 *
1177 * @param array $matches Single Regex Match.
1178 * @return string HTML A element with URI address.
1179 */
1180function _make_url_clickable_cb($matches) {
1181 $url = $matches[2];
1182 $url = esc_url($url);
1183 if ( empty($url) )
1184 return $matches[0];
1185 return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>";
1186}
1187
1188/**
1189 * Callback to convert URL match to HTML A element.
1190 *
1191 * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
1192 * make_clickable()}.
1193 *
1194 * @since 2.3.2
1195 * @access private
1196 *
1197 * @param array $matches Single Regex Match.
1198 * @return string HTML A element with URL address.
1199 */
1200function _make_web_ftp_clickable_cb($matches) {
1201 $ret = '';
1202 $dest = $matches[2];
1203 $dest = 'http://' . $dest;
1204 $dest = esc_url($dest);
1205 if ( empty($dest) )
1206 return $matches[0];
1207 // removed trailing [,;:] from URL
1208 if ( in_array(substr($dest, -1), array('.', ',', ';', ':')) === true ) {
1209 $ret = substr($dest, -1);
1210 $dest = substr($dest, 0, strlen($dest)-1);
1211 }
1212 return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>" . $ret;
1213}
1214
1215/**
1216 * Callback to convert email address match to HTML A element.
1217 *
1218 * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
1219 * make_clickable()}.
1220 *
1221 * @since 2.3.2
1222 * @access private
1223 *
1224 * @param array $matches Single Regex Match.
1225 * @return string HTML A element with email address.
1226 */
1227function _make_email_clickable_cb($matches) {
1228 $email = $matches[2] . '@' . $matches[3];
1229 return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
1230}
1231
1232/**
1233 * Convert plaintext URI to HTML links.
1234 *
1235 * Converts URI, www and ftp, and email addresses. Finishes by fixing links
1236 * within links.
1237 *
1238 * @since 0.71
1239 *
1240 * @param string $ret Content to convert URIs.
1241 * @return string Content with converted URIs.
1242 */
1243function make_clickable($ret) {
1244 $ret = ' ' . $ret;
1245 // in testing, using arrays here was found to be faster
1246 $ret = preg_replace_callback('#(?<=[\s>])(\()?([\w]+?://(?:[\w\\x80-\\xff\#$%&~/\-=?@\[\](+]|[.,;:](?![\s<])|(?(1)\)(?![\s<])|\)))+)#is', '_make_url_clickable_cb', $ret);
1247 $ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret);
1248 $ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret);
1249 // this one is not in an array because we need it to run last, for cleanup of accidental links within links
1250 $ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);
1251 $ret = trim($ret);
1252 return $ret;
1253}
1254
1255/**
1256 * Adds rel nofollow string to all HTML A elements in content.
1257 *
1258 * @since 1.5.0
1259 *
1260 * @param string $text Content that may contain HTML A elements.
1261 * @return string Converted content.
1262 */
1263function wp_rel_nofollow( $text ) {
1264 global $wpdb;
1265 // This is a pre save filter, so text is already escaped.
1266 $text = stripslashes($text);
1267 $text = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $text);
1268 $text = $wpdb->escape($text);
1269 return $text;
1270}
1271
1272/**
1273 * Callback to used to add rel=nofollow string to HTML A element.
1274 *
1275 * Will remove already existing rel="nofollow" and rel='nofollow' from the
1276 * string to prevent from invalidating (X)HTML.
1277 *
1278 * @since 2.3.0
1279 *
1280 * @param array $matches Single Match
1281 * @return string HTML A Element with rel nofollow.
1282 */
1283function wp_rel_nofollow_callback( $matches ) {
1284 $text = $matches[1];
1285 $text = str_replace(array(' rel="nofollow"', " rel='nofollow'"), '', $text);
1286 return "<a $text rel=\"nofollow\">";
1287}
1288
1289
1290/**
1291 * Convert one smiley code to the icon graphic file equivalent.
1292 *
1293 * Looks up one smiley code in the $wpsmiliestrans global array and returns an
1294 * <img> string for that smiley.
1295 *
1296 * @global array $wpsmiliestrans
1297 * @since 2.8.0
1298 *
1299 * @param string $smiley Smiley code to convert to image.
1300 * @return string Image string for smiley.
1301 */
1302function translate_smiley($smiley) {
1303 global $wpsmiliestrans;
1304
1305 if (count($smiley) == 0) {
1306 return '';
1307 }
1308
1309 $siteurl = get_option( 'siteurl' );
1310
1311 $smiley = trim(reset($smiley));
1312 $img = $wpsmiliestrans[$smiley];
1313 $smiley_masked = esc_attr($smiley);
1314
1315 return " <img src='$siteurl/wp-includes/images/smilies/$img' alt='$smiley_masked' class='wp-smiley' /> ";
1316}
1317
1318
1319/**
1320 * Convert text equivalent of smilies to images.
1321 *
1322 * Will only convert smilies if the option 'use_smilies' is true and the global
1323 * used in the function isn't empty.
1324 *
1325 * @since 0.71
1326 * @uses $wp_smiliessearch
1327 *
1328 * @param string $text Content to convert smilies from text.
1329 * @return string Converted content with text smilies replaced with images.
1330 */
1331function convert_smilies($text) {
1332 global $wp_smiliessearch;
1333 $output = '';
1334 if ( get_option('use_smilies') && !empty($wp_smiliessearch) ) {
1335 // HTML loop taken from texturize function, could possible be consolidated
1336 $textarr = preg_split("/(<.*>)/U", $text, -1, PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between
1337 $stop = count($textarr);// loop stuff
1338 for ($i = 0; $i < $stop; $i++) {
1339 $content = $textarr[$i];
1340 if ((strlen($content) > 0) && ('<' != $content{0})) { // If it's not a tag
1341 $content = preg_replace_callback($wp_smiliessearch, 'translate_smiley', $content);
1342 }
1343 $output .= $content;
1344 }
1345 } else {
1346 // return default text.
1347 $output = $text;
1348 }
1349 return $output;
1350}
1351
1352/**
1353 * Verifies that an email is valid.
1354 *
1355 * Does not grok i18n domains. Not RFC compliant.
1356 *
1357 * @since 0.71
1358 *
1359 * @param string $email Email address to verify.
1360 * @param boolean $check_dns Whether to check the DNS for the domain using checkdnsrr().
1361 * @return string|bool Either false or the valid email address.
1362 */
1363function is_email( $email, $check_dns = false ) {
1364 // Test for the minimum length the email can be
1365 if ( strlen( $email ) < 3 ) {
1366 return apply_filters( 'is_email', false, $email, 'email_too_short' );
1367 }
1368
1369 // Test for an @ character after the first position
1370 if ( strpos( $email, '@', 1 ) === false ) {
1371 return apply_filters( 'is_email', false, $email, 'email_no_at' );
1372 }
1373
1374 // Split out the local and domain parts
1375 list( $local, $domain ) = explode( '@', $email, 2 );
1376
1377 // LOCAL PART
1378 // Test for invalid characters
1379 if ( !preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local ) ) {
1380 return apply_filters( 'is_email', false, $email, 'local_invalid_chars' );
1381 }
1382
1383 // DOMAIN PART
1384 // Test for sequences of periods
1385 if ( preg_match( '/\.{2,}/', $domain ) ) {
1386 return apply_filters( 'is_email', false, $email, 'domain_period_sequence' );
1387 }
1388
1389 // Test for leading and trailing periods and whitespace
1390 if ( trim( $domain, " \t\n\r\0\x0B." ) !== $domain ) {
1391 return apply_filters( 'is_email', false, $email, 'domain_period_limits' );
1392 }
1393
1394 // Split the domain into subs
1395 $subs = explode( '.', $domain );
1396
1397 // Assume the domain will have at least two subs
1398 if ( 2 > count( $subs ) ) {
1399 return apply_filters( 'is_email', false, $email, 'domain_no_periods' );
1400 }
1401
1402 // Loop through each sub
1403 foreach ( $subs as $sub ) {
1404 // Test for leading and trailing hyphens and whitespace
1405 if ( trim( $sub, " \t\n\r\0\x0B-" ) !== $sub ) {
1406 return apply_filters( 'is_email', false, $email, 'sub_hyphen_limits' );
1407 }
1408
1409 // Test for invalid characters
1410 if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) {
1411 return apply_filters( 'is_email', false, $email, 'sub_invalid_chars' );
1412 }
1413 }
1414
1415 // DNS
1416 // Check the domain has a valid MX and A resource record
1417 if ( $check_dns && function_exists( 'checkdnsrr' ) && !( checkdnsrr( $domain . '.', 'MX' ) || checkdnsrr( $domain . '.', 'A' ) ) ) {
1418 return apply_filters( 'is_email', false, $email, 'dns_no_rr' );
1419 }
1420
1421 // Congratulations your email made it!
1422 return apply_filters( 'is_email', $email, $email, null );
1423}
1424
1425/**
1426 * Convert to ASCII from email subjects.
1427 *
1428 * @since 1.2.0
1429 * @usedby wp_mail() handles charsets in email subjects
1430 *
1431 * @param string $string Subject line
1432 * @return string Converted string to ASCII
1433 */
1434function wp_iso_descrambler($string) {
1435 /* this may only work with iso-8859-1, I'm afraid */
1436 if (!preg_match('#\=\?(.+)\?Q\?(.+)\?\=#i', $string, $matches)) {
1437 return $string;
1438 } else {
1439 $subject = str_replace('_', ' ', $matches[2]);
1440 $subject = preg_replace_callback('#\=([0-9a-f]{2})#i', create_function('$match', 'return chr(hexdec(strtolower($match[1])));'), $subject);
1441 return $subject;
1442 }
1443}
1444
1445/**
1446 * Returns a date in the GMT equivalent.
1447 *
1448 * Requires and returns a date in the Y-m-d H:i:s format. Simply subtracts the
1449 * value of the 'gmt_offset' option.
1450 *
1451 * @since 1.2.0
1452 *
1453 * @uses get_option() to retrieve the the value of 'gmt_offset'.
1454 * @param string $string The date to be converted.
1455 * @return string GMT version of the date provided.
1456 */
1457function get_gmt_from_date($string) {
1458 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);
1459 $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
1460 $string_gmt = gmdate('Y-m-d H:i:s', $string_time - get_option('gmt_offset') * 3600);
1461 return $string_gmt;
1462}
1463
1464/**
1465 * Converts a GMT date into the correct format for the blog.
1466 *
1467 * Requires and returns in the Y-m-d H:i:s format. Simply adds the value of
1468 * gmt_offset.
1469 *
1470 * @since 1.2.0
1471 *
1472 * @param string $string The date to be converted.
1473 * @return string Formatted date relative to the GMT offset.
1474 */
1475function get_date_from_gmt($string) {
1476 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);
1477 $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
1478 $string_localtime = gmdate('Y-m-d H:i:s', $string_time + get_option('gmt_offset')*3600);
1479 return $string_localtime;
1480}
1481
1482/**
1483 * Computes an offset in seconds from an iso8601 timezone.
1484 *
1485 * @since 1.5.0
1486 *
1487 * @param string $timezone Either 'Z' for 0 offset or '±hhmm'.
1488 * @return int|float The offset in seconds.
1489 */
1490function iso8601_timezone_to_offset($timezone) {
1491 // $timezone is either 'Z' or '[+|-]hhmm'
1492 if ($timezone == 'Z') {
1493 $offset = 0;
1494 } else {
1495 $sign = (substr($timezone, 0, 1) == '+') ? 1 : -1;
1496 $hours = intval(substr($timezone, 1, 2));
1497 $minutes = intval(substr($timezone, 3, 4)) / 60;
1498 $offset = $sign * 3600 * ($hours + $minutes);
1499 }
1500 return $offset;
1501}
1502
1503/**
1504 * Converts an iso8601 date to MySQL DateTime format used by post_date[_gmt].
1505 *
1506 * @since 1.5.0
1507 *
1508 * @param string $date_string Date and time in ISO 8601 format {@link http://en.wikipedia.org/wiki/ISO_8601}.
1509 * @param string $timezone Optional. If set to GMT returns the time minus gmt_offset. Default is 'user'.
1510 * @return string The date and time in MySQL DateTime format - Y-m-d H:i:s.
1511 */
1512function iso8601_to_datetime($date_string, $timezone = 'user') {
1513 $timezone = strtolower($timezone);
1514
1515 if ($timezone == 'gmt') {
1516
1517 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);
1518
1519 if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset
1520 $offset = iso8601_timezone_to_offset($date_bits[7]);
1521 } else { // we don't have a timezone, so we assume user local timezone (not server's!)
1522 $offset = 3600 * get_option('gmt_offset');
1523 }
1524
1525 $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
1526 $timestamp -= $offset;
1527
1528 return gmdate('Y-m-d H:i:s', $timestamp);
1529
1530 } else if ($timezone == 'user') {
1531 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);
1532 }
1533}
1534
1535/**
1536 * Adds a element attributes to open links in new windows.
1537 *
1538 * Comment text in popup windows should be filtered through this. Right now it's
1539 * a moderately dumb function, ideally it would detect whether a target or rel
1540 * attribute was already there and adjust its actions accordingly.
1541 *
1542 * @since 0.71
1543 *
1544 * @param string $text Content to replace links to open in a new window.
1545 * @return string Content that has filtered links.
1546 */
1547function popuplinks($text) {
1548 $text = preg_replace('/<a (.+?)>/i', "<a $1 target='_blank' rel='external'>", $text);
1549 return $text;
1550}
1551
1552/**
1553 * Strips out all characters that are not allowable in an email.
1554 *
1555 * @since 1.5.0
1556 *
1557 * @param string $email Email address to filter.
1558 * @return string Filtered email address.
1559 */
1560function sanitize_email( $email ) {
1561 // Test for the minimum length the email can be
1562 if ( strlen( $email ) < 3 ) {
1563 return apply_filters( 'sanitize_email', '', $email, 'email_too_short' );
1564 }
1565
1566 // Test for an @ character after the first position
1567 if ( strpos( $email, '@', 1 ) === false ) {
1568 return apply_filters( 'sanitize_email', '', $email, 'email_no_at' );
1569 }
1570
1571 // Split out the local and domain parts
1572 list( $local, $domain ) = explode( '@', $email, 2 );
1573
1574 // LOCAL PART
1575 // Test for invalid characters
1576 $local = preg_replace( '/[^a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]/', '', $local );
1577 if ( '' === $local ) {
1578 return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' );
1579 }
1580
1581 // DOMAIN PART
1582 // Test for sequences of periods
1583 $domain = preg_replace( '/\.{2,}/', '', $domain );
1584 if ( '' === $domain ) {
1585 return apply_filters( 'sanitize_email', '', $email, 'domain_period_sequence' );
1586 }
1587
1588 // Test for leading and trailing periods and whitespace
1589 $domain = trim( $domain, " \t\n\r\0\x0B." );
1590 if ( '' === $domain ) {
1591 return apply_filters( 'sanitize_email', '', $email, 'domain_period_limits' );
1592 }
1593
1594 // Split the domain into subs
1595 $subs = explode( '.', $domain );
1596
1597 // Assume the domain will have at least two subs
1598 if ( 2 > count( $subs ) ) {
1599 return apply_filters( 'sanitize_email', '', $email, 'domain_no_periods' );
1600 }
1601
1602 // Create an array that will contain valid subs
1603 $new_subs = array();
1604
1605 // Loop through each sub
1606 foreach ( $subs as $sub ) {
1607 // Test for leading and trailing hyphens
1608 $sub = trim( $sub, " \t\n\r\0\x0B-" );
1609
1610 // Test for invalid characters
1611 $sub = preg_replace( '/^[^a-z0-9-]+$/i', '', $sub );
1612
1613 // If there's anything left, add it to the valid subs
1614 if ( '' !== $sub ) {
1615 $new_subs[] = $sub;
1616 }
1617 }
1618
1619 // If there aren't 2 or more valid subs
1620 if ( 2 > count( $new_subs ) ) {
1621 return apply_filters( 'sanitize_email', '', $email, 'domain_no_valid_subs' );
1622 }
1623
1624 // Join valid subs into the new domain
1625 $domain = join( '.', $new_subs );
1626
1627 // Put the email back together
1628 $email = $local . '@' . $domain;
1629
1630 // Congratulations your email made it!
1631 return apply_filters( 'sanitize_email', $email, $email, null );
1632}
1633
1634/**
1635 * Determines the difference between two timestamps.
1636 *
1637 * The difference is returned in a human readable format such as "1 hour",
1638 * "5 mins", "2 days".
1639 *
1640 * @since 1.5.0
1641 *
1642 * @param int $from Unix timestamp from which the difference begins.
1643 * @param int $to Optional. Unix timestamp to end the time difference. Default becomes time() if not set.
1644 * @return string Human readable time difference.
1645 */
1646function human_time_diff( $from, $to = '' ) {
1647 if ( empty($to) )
1648 $to = time();
1649 $diff = (int) abs($to - $from);
1650 if ($diff <= 3600) {
1651 $mins = round($diff / 60);
1652 if ($mins <= 1) {
1653 $mins = 1;
1654 }
1655 $since = sprintf(_n('%s min', '%s mins', $mins), $mins);
1656 } else if (($diff <= 86400) && ($diff > 3600)) {
1657 $hours = round($diff / 3600);
1658 if ($hours <= 1) {
1659 $hours = 1;
1660 }
1661 $since = sprintf(_n('%s hour', '%s hours', $hours), $hours);
1662 } elseif ($diff >= 86400) {
1663 $days = round($diff / 86400);
1664 if ($days <= 1) {
1665 $days = 1;
1666 }
1667 $since = sprintf(_n('%s day', '%s days', $days), $days);
1668 }
1669 return $since;
1670}
1671
1672/**
1673 * Generates an excerpt from the content, if needed.
1674 *
1675 * The excerpt word amount will be 55 words and if the amount is greater than
1676 * that, then the string '[...]' will be appended to the excerpt. If the string
1677 * is less than 55 words, then the content will be returned as is.
1678 *
1679 * @since 1.5.0
1680 *
1681 * @param string $text The exerpt. If set to empty an excerpt is generated.
1682 * @return string The excerpt.
1683 */
1684function wp_trim_excerpt($text) {
1685 $raw_excerpt = $text;
1686 if ( '' == $text ) {
1687 $text = get_the_content('');
1688
1689 $text = strip_shortcodes( $text );
1690
1691 $text = apply_filters('the_content', $text);
1692 $text = str_replace(']]>', ']]&gt;', $text);
1693 $text = strip_tags($text);
1694 $excerpt_length = apply_filters('excerpt_length', 55);
1695 $words = explode(' ', $text, $excerpt_length + 1);
1696 if (count($words) > $excerpt_length) {
1697 array_pop($words);
1698 array_push($words, '[...]');
1699 $text = implode(' ', $words);
1700 }
1701 }
1702 return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
1703}
1704
1705/**
1706 * Converts named entities into numbered entities.
1707 *
1708 * @since 1.5.1
1709 *
1710 * @param string $text The text within which entities will be converted.
1711 * @return string Text with converted entities.
1712 */
1713function ent2ncr($text) {
1714 $to_ncr = array(
1715 '&quot;' => '&#34;',
1716 '&amp;' => '&#38;',
1717 '&frasl;' => '&#47;',
1718 '&lt;' => '&#60;',
1719 '&gt;' => '&#62;',
1720 '|' => '&#124;',
1721 '&nbsp;' => '&#160;',
1722 '&iexcl;' => '&#161;',
1723 '&cent;' => '&#162;',
1724 '&pound;' => '&#163;',
1725 '&curren;' => '&#164;',
1726 '&yen;' => '&#165;',
1727 '&brvbar;' => '&#166;',
1728 '&brkbar;' => '&#166;',
1729 '&sect;' => '&#167;',
1730 '&uml;' => '&#168;',
1731 '&die;' => '&#168;',
1732 '&copy;' => '&#169;',
1733 '&ordf;' => '&#170;',
1734 '&laquo;' => '&#171;',
1735 '&not;' => '&#172;',
1736 '&shy;' => '&#173;',
1737 '&reg;' => '&#174;',
1738 '&macr;' => '&#175;',
1739 '&hibar;' => '&#175;',
1740 '&deg;' => '&#176;',
1741 '&plusmn;' => '&#177;',
1742 '&sup2;' => '&#178;',
1743 '&sup3;' => '&#179;',
1744 '&acute;' => '&#180;',
1745 '&micro;' => '&#181;',
1746 '&para;' => '&#182;',
1747 '&middot;' => '&#183;',
1748 '&cedil;' => '&#184;',
1749 '&sup1;' => '&#185;',
1750 '&ordm;' => '&#186;',
1751 '&raquo;' => '&#187;',
1752 '&frac14;' => '&#188;',
1753 '&frac12;' => '&#189;',
1754 '&frac34;' => '&#190;',
1755 '&iquest;' => '&#191;',
1756 '&Agrave;' => '&#192;',
1757 '&Aacute;' => '&#193;',
1758 '&Acirc;' => '&#194;',
1759 '&Atilde;' => '&#195;',
1760 '&Auml;' => '&#196;',
1761 '&Aring;' => '&#197;',
1762 '&AElig;' => '&#198;',
1763 '&Ccedil;' => '&#199;',
1764 '&Egrave;' => '&#200;',
1765 '&Eacute;' => '&#201;',
1766 '&Ecirc;' => '&#202;',
1767 '&Euml;' => '&#203;',
1768 '&Igrave;' => '&#204;',
1769 '&Iacute;' => '&#205;',
1770 '&Icirc;' => '&#206;',
1771 '&Iuml;' => '&#207;',
1772 '&ETH;' => '&#208;',
1773 '&Ntilde;' => '&#209;',
1774 '&Ograve;' => '&#210;',
1775 '&Oacute;' => '&#211;',
1776 '&Ocirc;' => '&#212;',
1777 '&Otilde;' => '&#213;',
1778 '&Ouml;' => '&#214;',
1779 '&times;' => '&#215;',
1780 '&Oslash;' => '&#216;',
1781 '&Ugrave;' => '&#217;',
1782 '&Uacute;' => '&#218;',
1783 '&Ucirc;' => '&#219;',
1784 '&Uuml;' => '&#220;',
1785 '&Yacute;' => '&#221;',
1786 '&THORN;' => '&#222;',
1787 '&szlig;' => '&#223;',
1788 '&agrave;' => '&#224;',
1789 '&aacute;' => '&#225;',
1790 '&acirc;' => '&#226;',
1791 '&atilde;' => '&#227;',
1792 '&auml;' => '&#228;',
1793 '&aring;' => '&#229;',
1794 '&aelig;' => '&#230;',
1795 '&ccedil;' => '&#231;',
1796 '&egrave;' => '&#232;',
1797 '&eacute;' => '&#233;',
1798 '&ecirc;' => '&#234;',
1799 '&euml;' => '&#235;',
1800 '&igrave;' => '&#236;',
1801 '&iacute;' => '&#237;',
1802 '&icirc;' => '&#238;',
1803 '&iuml;' => '&#239;',
1804 '&eth;' => '&#240;',
1805 '&ntilde;' => '&#241;',
1806 '&ograve;' => '&#242;',
1807 '&oacute;' => '&#243;',
1808 '&ocirc;' => '&#244;',
1809 '&otilde;' => '&#245;',
1810 '&ouml;' => '&#246;',
1811 '&divide;' => '&#247;',
1812 '&oslash;' => '&#248;',
1813 '&ugrave;' => '&#249;',
1814 '&uacute;' => '&#250;',
1815 '&ucirc;' => '&#251;',
1816 '&uuml;' => '&#252;',
1817 '&yacute;' => '&#253;',
1818 '&thorn;' => '&#254;',
1819 '&yuml;' => '&#255;',
1820 '&OElig;' => '&#338;',
1821 '&oelig;' => '&#339;',
1822 '&Scaron;' => '&#352;',
1823 '&scaron;' => '&#353;',
1824 '&Yuml;' => '&#376;',
1825 '&fnof;' => '&#402;',
1826 '&circ;' => '&#710;',
1827 '&tilde;' => '&#732;',
1828 '&Alpha;' => '&#913;',
1829 '&Beta;' => '&#914;',
1830 '&Gamma;' => '&#915;',
1831 '&Delta;' => '&#916;',
1832 '&Epsilon;' => '&#917;',
1833 '&Zeta;' => '&#918;',
1834 '&Eta;' => '&#919;',
1835 '&Theta;' => '&#920;',
1836 '&Iota;' => '&#921;',
1837 '&Kappa;' => '&#922;',
1838 '&Lambda;' => '&#923;',
1839 '&Mu;' => '&#924;',
1840 '&Nu;' => '&#925;',
1841 '&Xi;' => '&#926;',
1842 '&Omicron;' => '&#927;',
1843 '&Pi;' => '&#928;',
1844 '&Rho;' => '&#929;',
1845 '&Sigma;' => '&#931;',
1846 '&Tau;' => '&#932;',
1847 '&Upsilon;' => '&#933;',
1848 '&Phi;' => '&#934;',
1849 '&Chi;' => '&#935;',
1850 '&Psi;' => '&#936;',
1851 '&Omega;' => '&#937;',
1852 '&alpha;' => '&#945;',
1853 '&beta;' => '&#946;',
1854 '&gamma;' => '&#947;',
1855 '&delta;' => '&#948;',
1856 '&epsilon;' => '&#949;',
1857 '&zeta;' => '&#950;',
1858 '&eta;' => '&#951;',
1859 '&theta;' => '&#952;',
1860 '&iota;' => '&#953;',
1861 '&kappa;' => '&#954;',
1862 '&lambda;' => '&#955;',
1863 '&mu;' => '&#956;',
1864 '&nu;' => '&#957;',
1865 '&xi;' => '&#958;',
1866 '&omicron;' => '&#959;',
1867 '&pi;' => '&#960;',
1868 '&rho;' => '&#961;',
1869 '&sigmaf;' => '&#962;',
1870 '&sigma;' => '&#963;',
1871 '&tau;' => '&#964;',
1872 '&upsilon;' => '&#965;',
1873 '&phi;' => '&#966;',
1874 '&chi;' => '&#967;',
1875 '&psi;' => '&#968;',
1876 '&omega;' => '&#969;',
1877 '&thetasym;' => '&#977;',
1878 '&upsih;' => '&#978;',
1879 '&piv;' => '&#982;',
1880 '&ensp;' => '&#8194;',
1881 '&emsp;' => '&#8195;',
1882 '&thinsp;' => '&#8201;',
1883 '&zwnj;' => '&#8204;',
1884 '&zwj;' => '&#8205;',
1885 '&lrm;' => '&#8206;',
1886 '&rlm;' => '&#8207;',
1887 '&ndash;' => '&#8211;',
1888 '&mdash;' => '&#8212;',
1889 '&lsquo;' => '&#8216;',
1890 '&rsquo;' => '&#8217;',
1891 '&sbquo;' => '&#8218;',
1892 '&ldquo;' => '&#8220;',
1893 '&rdquo;' => '&#8221;',
1894 '&bdquo;' => '&#8222;',
1895 '&dagger;' => '&#8224;',
1896 '&Dagger;' => '&#8225;',
1897 '&bull;' => '&#8226;',
1898 '&hellip;' => '&#8230;',
1899 '&permil;' => '&#8240;',
1900 '&prime;' => '&#8242;',
1901 '&Prime;' => '&#8243;',
1902 '&lsaquo;' => '&#8249;',
1903 '&rsaquo;' => '&#8250;',
1904 '&oline;' => '&#8254;',
1905 '&frasl;' => '&#8260;',
1906 '&euro;' => '&#8364;',
1907 '&image;' => '&#8465;',
1908 '&weierp;' => '&#8472;',
1909 '&real;' => '&#8476;',
1910 '&trade;' => '&#8482;',
1911 '&alefsym;' => '&#8501;',
1912 '&crarr;' => '&#8629;',
1913 '&lArr;' => '&#8656;',
1914 '&uArr;' => '&#8657;',
1915 '&rArr;' => '&#8658;',
1916 '&dArr;' => '&#8659;',
1917 '&hArr;' => '&#8660;',
1918 '&forall;' => '&#8704;',
1919 '&part;' => '&#8706;',
1920 '&exist;' => '&#8707;',
1921 '&empty;' => '&#8709;',
1922 '&nabla;' => '&#8711;',
1923 '&isin;' => '&#8712;',
1924 '&notin;' => '&#8713;',
1925 '&ni;' => '&#8715;',
1926 '&prod;' => '&#8719;',
1927 '&sum;' => '&#8721;',
1928 '&minus;' => '&#8722;',
1929 '&lowast;' => '&#8727;',
1930 '&radic;' => '&#8730;',
1931 '&prop;' => '&#8733;',
1932 '&infin;' => '&#8734;',
1933 '&ang;' => '&#8736;',
1934 '&and;' => '&#8743;',
1935 '&or;' => '&#8744;',
1936 '&cap;' => '&#8745;',
1937 '&cup;' => '&#8746;',
1938 '&int;' => '&#8747;',
1939 '&there4;' => '&#8756;',
1940 '&sim;' => '&#8764;',
1941 '&cong;' => '&#8773;',
1942 '&asymp;' => '&#8776;',
1943 '&ne;' => '&#8800;',
1944 '&equiv;' => '&#8801;',
1945 '&le;' => '&#8804;',
1946 '&ge;' => '&#8805;',
1947 '&sub;' => '&#8834;',
1948 '&sup;' => '&#8835;',
1949 '&nsub;' => '&#8836;',
1950 '&sube;' => '&#8838;',
1951 '&supe;' => '&#8839;',
1952 '&oplus;' => '&#8853;',
1953 '&otimes;' => '&#8855;',
1954 '&perp;' => '&#8869;',
1955 '&sdot;' => '&#8901;',
1956 '&lceil;' => '&#8968;',
1957 '&rceil;' => '&#8969;',
1958 '&lfloor;' => '&#8970;',
1959 '&rfloor;' => '&#8971;',
1960 '&lang;' => '&#9001;',
1961 '&rang;' => '&#9002;',
1962 '&larr;' => '&#8592;',
1963 '&uarr;' => '&#8593;',
1964 '&rarr;' => '&#8594;',
1965 '&darr;' => '&#8595;',
1966 '&harr;' => '&#8596;',
1967 '&loz;' => '&#9674;',
1968 '&spades;' => '&#9824;',
1969 '&clubs;' => '&#9827;',
1970 '&hearts;' => '&#9829;',
1971 '&diams;' => '&#9830;'
1972 );
1973
1974 return str_replace( array_keys($to_ncr), array_values($to_ncr), $text );
1975}
1976
1977/**
1978 * Formats text for the rich text editor.
1979 *
1980 * The filter 'richedit_pre' is applied here. If $text is empty the filter will
1981 * be applied to an empty string.
1982 *
1983 * @since 2.0.0
1984 *
1985 * @param string $text The text to be formatted.
1986 * @return string The formatted text after filter is applied.
1987 */
1988function wp_richedit_pre($text) {
1989 // Filtering a blank results in an annoying <br />\n
1990 if ( empty($text) ) return apply_filters('richedit_pre', '');
1991
1992 $output = convert_chars($text);
1993 $output = wpautop($output);
1994 $output = htmlspecialchars($output, ENT_NOQUOTES);
1995
1996 return apply_filters('richedit_pre', $output);
1997}
1998
1999/**
2000 * Formats text for the HTML editor.
2001 *
2002 * Unless $output is empty it will pass through htmlspecialchars before the
2003 * 'htmledit_pre' filter is applied.
2004 *
2005 * @since 2.5.0
2006 *
2007 * @param string $output The text to be formatted.
2008 * @return string Formatted text after filter applied.
2009 */
2010function wp_htmledit_pre($output) {
2011 if ( !empty($output) )
2012 $output = htmlspecialchars($output, ENT_NOQUOTES); // convert only < > &
2013
2014 return apply_filters('htmledit_pre', $output);
2015}
2016
2017/**
2018 * Checks and cleans a URL.
2019 *
2020 * A number of characters are removed from the URL. If the URL is for displaying
2021 * (the default behaviour) amperstands are also replaced. The 'esc_url' filter
2022 * is applied to the returned cleaned URL.
2023 *
2024 * @since 1.2.0
2025 * @uses wp_kses_bad_protocol() To only permit protocols in the URL set
2026 * via $protocols or the common ones set in the function.
2027 *
2028 * @param string $url The URL to be cleaned.
2029 * @param array $protocols Optional. An array of acceptable protocols.
2030 * Defaults to 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet' if not set.
2031 * @param string $context Optional. How the URL will be used. Default is 'display'.
2032 * @return string The cleaned $url after the 'cleaned_url' filter is applied.
2033 */
2034function clean_url( $url, $protocols = null, $context = 'display' ) {
2035 $original_url = $url;
2036
2037 if ('' == $url) return $url;
2038 $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url);
2039 $strip = array('%0d', '%0a');
2040 $url = str_replace($strip, '', $url);
2041 $url = str_replace(';//', '://', $url);
2042 /* If the URL doesn't appear to contain a scheme, we
2043 * presume it needs http:// appended (unless a relative
2044 * link starting with / or a php file).
2045 */
2046 if ( strpos($url, ':') === false &&
2047 substr( $url, 0, 1 ) != '/' && substr( $url, 0, 1 ) != '#' && !preg_match('/^[a-z0-9-]+?\.php/i', $url) )
2048 $url = 'http://' . $url;
2049
2050 // Replace ampersands and single quotes only when displaying.
2051 if ( 'display' == $context ) {
2052 $url = preg_replace('/&([^#])(?![a-z]{2,8};)/', '&#038;$1', $url);
2053 $url = str_replace( "'", '&#039;', $url );
2054 }
2055
2056 if ( !is_array($protocols) )
2057 $protocols = array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet');
2058 if ( wp_kses_bad_protocol( $url, $protocols ) != $url )
2059 return '';
2060
2061 return apply_filters('clean_url', $url, $original_url, $context);
2062}
2063
2064/**
2065 * Checks and cleans a URL.
2066 *
2067 * A number of characters are removed from the URL. If the URL is for displaying
2068 * (the default behaviour) amperstands are also replaced. The 'esc_url' filter
2069 * is applied to the returned cleaned URL.
2070 *
2071 * @since 2.8.0
2072 * @uses esc_url()
2073 * @uses wp_kses_bad_protocol() To only permit protocols in the URL set
2074 * via $protocols or the common ones set in the function.
2075 *
2076 * @param string $url The URL to be cleaned.
2077 * @param array $protocols Optional. An array of acceptable protocols.
2078 * Defaults to 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet' if not set.
2079 * @return string The cleaned $url after the 'cleaned_url' filter is applied.
2080 */
2081function esc_url( $url, $protocols = null ) {
2082 return clean_url( $url, $protocols, 'display' );
2083}
2084
2085/**
2086 * Performs esc_url() for database usage.
2087 *
2088 * @see esc_url()
2089 * @see esc_url()
2090 *
2091 * @since 2.8.0
2092 *
2093 * @param string $url The URL to be cleaned.
2094 * @param array $protocols An array of acceptable protocols.
2095 * @return string The cleaned URL.
2096 */
2097function esc_url_raw( $url, $protocols = null ) {
2098 return clean_url( $url, $protocols, 'db' );
2099}
2100
2101/**
2102 * Performs esc_url() for database or redirect usage.
2103 *
2104 * @see esc_url()
2105 * @deprecated 2.8.0
2106 *
2107 * @since 2.3.1
2108 *
2109 * @param string $url The URL to be cleaned.
2110 * @param array $protocols An array of acceptable protocols.
2111 * @return string The cleaned URL.
2112 */
2113function sanitize_url( $url, $protocols = null ) {
2114 return clean_url( $url, $protocols, 'db' );
2115}
2116
2117/**
2118 * Convert entities, while preserving already-encoded entities.
2119 *
2120 * @link http://www.php.net/htmlentities Borrowed from the PHP Manual user notes.
2121 *
2122 * @since 1.2.2
2123 *
2124 * @param string $myHTML The text to be converted.
2125 * @return string Converted text.
2126 */
2127function htmlentities2($myHTML) {
2128 $translation_table = get_html_translation_table( HTML_ENTITIES, ENT_QUOTES );
2129 $translation_table[chr(38)] = '&';
2130 return preg_replace( "/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/", "&amp;", strtr($myHTML, $translation_table) );
2131}
2132
2133/**
2134 * Escape single quotes, specialchar double quotes, and fix line endings.
2135 *
2136 * The filter 'js_escape' is also applied here.
2137 *
2138 * @since 2.8.0
2139 *
2140 * @param string $text The text to be escaped.
2141 * @return string Escaped text.
2142 */
2143function esc_js( $text ) {
2144 $safe_text = wp_check_invalid_utf8( $text );
2145 $safe_text = _wp_specialchars( $safe_text, ENT_COMPAT );
2146 $safe_text = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes( $safe_text ) );
2147 $safe_text = preg_replace( "/\r?\n/", "\\n", addslashes( $safe_text ) );
2148 return apply_filters( 'js_escape', $safe_text, $text );
2149}
2150
2151/**
2152 * Escape single quotes, specialchar double quotes, and fix line endings.
2153 *
2154 * The filter 'js_escape' is also applied by esc_js()
2155 *
2156 * @since 2.0.4
2157 *
2158 * @deprecated 2.8.0
2159 * @see esc_js()
2160 *
2161 * @param string $text The text to be escaped.
2162 * @return string Escaped text.
2163 */
2164function js_escape( $text ) {
2165 return esc_js( $text );
2166}
2167
2168/**
2169 * Escaping for HTML blocks.
2170 *
2171 * @since 2.8.0
2172 *
2173 * @param string $text
2174 * @return string
2175 */
2176function esc_html( $text ) {
2177 $safe_text = wp_check_invalid_utf8( $text );
2178 $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
2179 return apply_filters( 'esc_html', $safe_text, $text );
2180 return $text;
2181}
2182
2183/**
2184 * Escaping for HTML blocks
2185 * @deprecated 2.8.0
2186 * @see esc_html()
2187 */
2188function wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
2189 if ( func_num_args() > 1 ) { // Maintain backwards compat for people passing additional args
2190 $args = func_get_args();
2191 return call_user_func_array( '_wp_specialchars', $args );
2192 } else {
2193 return esc_html( $string );
2194 }
2195}
2196
2197/**
2198 * Escaping for HTML attributes.
2199 *
2200 * @since 2.8.0
2201 *
2202 * @param string $text
2203 * @return string
2204 */
2205function esc_attr( $text ) {
2206 $safe_text = wp_check_invalid_utf8( $text );
2207 $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
2208 return apply_filters( 'attribute_escape', $safe_text, $text );
2209}
2210
2211/**
2212 * Escaping for HTML attributes.
2213 *
2214 * @since 2.0.6
2215 *
2216 * @deprecated 2.8.0
2217 * @see esc_attr()
2218 *
2219 * @param string $text
2220 * @return string
2221 */
2222function attribute_escape( $text ) {
2223 return esc_attr( $text );
2224}
2225
2226/**
2227 * Escape a HTML tag name.
2228 *
2229 * @since 2.5.0
2230 *
2231 * @param string $tag_name
2232 * @return string
2233 */
2234function tag_escape($tag_name) {
2235 $safe_tag = strtolower( preg_replace('/[^a-zA-Z_:]/', '', $tag_name) );
2236 return apply_filters('tag_escape', $safe_tag, $tag_name);
2237}
2238
2239/**
2240 * Escapes text for SQL LIKE special characters % and _.
2241 *
2242 * @since 2.5.0
2243 *
2244 * @param string $text The text to be escaped.
2245 * @return string text, safe for inclusion in LIKE query.
2246 */
2247function like_escape($text) {
2248 return str_replace(array("%", "_"), array("\\%", "\\_"), $text);
2249}
2250
2251/**
2252 * Convert full URL paths to absolute paths.
2253 *
2254 * Removes the http or https protocols and the domain. Keeps the path '/' at the
2255 * beginning, so it isn't a true relative link, but from the web root base.
2256 *
2257 * @since 2.1.0
2258 *
2259 * @param string $link Full URL path.
2260 * @return string Absolute path.
2261 */
2262function wp_make_link_relative( $link ) {
2263 return preg_replace( '|https?://[^/]+(/.*)|i', '$1', $link );
2264}
2265
2266/**
2267 * Sanitises various option values based on the nature of the option.
2268 *
2269 * This is basically a switch statement which will pass $value through a number
2270 * of functions depending on the $option.
2271 *
2272 * @since 2.0.5
2273 *
2274 * @param string $option The name of the option.
2275 * @param string $value The unsanitised value.
2276 * @return string Sanitized value.
2277 */
2278function sanitize_option($option, $value) {
2279
2280 switch ($option) {
2281 case 'admin_email':
2282 $value = sanitize_email($value);
2283 break;
2284
2285 case 'thumbnail_size_w':
2286 case 'thumbnail_size_h':
2287 case 'medium_size_w':
2288 case 'medium_size_h':
2289 case 'large_size_w':
2290 case 'large_size_h':
2291 case 'default_post_edit_rows':
2292 case 'mailserver_port':
2293 case 'comment_max_links':
2294 case 'page_on_front':
2295 case 'rss_excerpt_length':
2296 case 'default_category':
2297 case 'default_email_category':
2298 case 'default_link_category':
2299 case 'close_comments_days_old':
2300 case 'comments_per_page':
2301 case 'thread_comments_depth':
2302 $value = abs((int) $value);
2303 break;
2304
2305 case 'posts_per_page':
2306 case 'posts_per_rss':
2307 $value = (int) $value;
2308 if ( empty($value) ) $value = 1;
2309 if ( $value < -1 ) $value = abs($value);
2310 break;
2311
2312 case 'default_ping_status':
2313 case 'default_comment_status':
2314 // Options that if not there have 0 value but need to be something like "closed"
2315 if ( $value == '0' || $value == '')
2316 $value = 'closed';
2317 break;
2318
2319 case 'blogdescription':
2320 case 'blogname':
2321 $value = addslashes($value);
2322 $value = wp_filter_post_kses( $value ); // calls stripslashes then addslashes
2323 $value = stripslashes($value);
2324 $value = esc_html( $value );
2325 break;
2326
2327 case 'blog_charset':
2328 $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value); // strips slashes
2329 break;
2330
2331 case 'date_format':
2332 case 'time_format':
2333 case 'mailserver_url':
2334 case 'mailserver_login':
2335 case 'mailserver_pass':
2336 case 'ping_sites':
2337 case 'upload_path':
2338 $value = strip_tags($value);
2339 $value = addslashes($value);
2340 $value = wp_filter_kses($value); // calls stripslashes then addslashes
2341 $value = stripslashes($value);
2342 break;
2343
2344 case 'gmt_offset':
2345 $value = preg_replace('/[^0-9:.-]/', '', $value); // strips slashes
2346 break;
2347
2348 case 'siteurl':
2349 case 'home':
2350 $value = stripslashes($value);
2351 $value = esc_url($value);
2352 break;
2353 default :
2354 $value = apply_filters("sanitize_option_{$option}", $value, $option);
2355 break;
2356 }
2357
2358 return $value;
2359}
2360
2361/**
2362 * Parses a string into variables to be stored in an array.
2363 *
2364 * Uses {@link http://www.php.net/parse_str parse_str()} and stripslashes if
2365 * {@link http://www.php.net/magic_quotes magic_quotes_gpc} is on.
2366 *
2367 * @since 2.2.1
2368 * @uses apply_filters() for the 'wp_parse_str' filter.
2369 *
2370 * @param string $string The string to be parsed.
2371 * @param array $array Variables will be stored in this array.
2372 */
2373function wp_parse_str( $string, &$array ) {
2374 parse_str( $string, $array );
2375 if ( get_magic_quotes_gpc() )
2376 $array = stripslashes_deep( $array );
2377 $array = apply_filters( 'wp_parse_str', $array );
2378}
2379
2380/**
2381 * Convert lone less than signs.
2382 *
2383 * KSES already converts lone greater than signs.
2384 *
2385 * @uses wp_pre_kses_less_than_callback in the callback function.
2386 * @since 2.3.0
2387 *
2388 * @param string $text Text to be converted.
2389 * @return string Converted text.
2390 */
2391function wp_pre_kses_less_than( $text ) {
2392 return preg_replace_callback('%<[^>]*?((?=<)|>|$)%', 'wp_pre_kses_less_than_callback', $text);
2393}
2394
2395/**
2396 * Callback function used by preg_replace.
2397 *
2398 * @uses esc_html to format the $matches text.
2399 * @since 2.3.0
2400 *
2401 * @param array $matches Populated by matches to preg_replace.
2402 * @return string The text returned after esc_html if needed.
2403 */
2404function wp_pre_kses_less_than_callback( $matches ) {
2405 if ( false === strpos($matches[0], '>') )
2406 return esc_html($matches[0]);
2407 return $matches[0];
2408}
2409
2410/**
2411 * WordPress implementation of PHP sprintf() with filters.
2412 *
2413 * @since 2.5.0
2414 * @link http://www.php.net/sprintf
2415 *
2416 * @param string $pattern The string which formatted args are inserted.
2417 * @param mixed $args,... Arguments to be formatted into the $pattern string.
2418 * @return string The formatted string.
2419 */
2420function wp_sprintf( $pattern ) {
2421 $args = func_get_args( );
2422 $len = strlen($pattern);
2423 $start = 0;
2424 $result = '';
2425 $arg_index = 0;
2426 while ( $len > $start ) {
2427 // Last character: append and break
2428 if ( strlen($pattern) - 1 == $start ) {
2429 $result .= substr($pattern, -1);
2430 break;
2431 }
2432
2433 // Literal %: append and continue
2434 if ( substr($pattern, $start, 2) == '%%' ) {
2435 $start += 2;
2436 $result .= '%';
2437 continue;
2438 }
2439
2440 // Get fragment before next %
2441 $end = strpos($pattern, '%', $start + 1);
2442 if ( false === $end )
2443 $end = $len;
2444 $fragment = substr($pattern, $start, $end - $start);
2445
2446 // Fragment has a specifier
2447 if ( $pattern{$start} == '%' ) {
2448 // Find numbered arguments or take the next one in order
2449 if ( preg_match('/^%(\d+)\$/', $fragment, $matches) ) {
2450 $arg = isset($args[$matches[1]]) ? $args[$matches[1]] : '';
2451 $fragment = str_replace("%{$matches[1]}$", '%', $fragment);
2452 } else {
2453 ++$arg_index;
2454 $arg = isset($args[$arg_index]) ? $args[$arg_index] : '';
2455 }
2456
2457 // Apply filters OR sprintf
2458 $_fragment = apply_filters( 'wp_sprintf', $fragment, $arg );
2459 if ( $_fragment != $fragment )
2460 $fragment = $_fragment;
2461 else
2462 $fragment = sprintf($fragment, strval($arg) );
2463 }
2464
2465 // Append to result and move to next fragment
2466 $result .= $fragment;
2467 $start = $end;
2468 }
2469 return $result;
2470}
2471
2472/**
2473 * Localize list items before the rest of the content.
2474 *
2475 * The '%l' must be at the first characters can then contain the rest of the
2476 * content. The list items will have ', ', ', and', and ' and ' added depending
2477 * on the amount of list items in the $args parameter.
2478 *
2479 * @since 2.5.0
2480 *
2481 * @param string $pattern Content containing '%l' at the beginning.
2482 * @param array $args List items to prepend to the content and replace '%l'.
2483 * @return string Localized list items and rest of the content.
2484 */
2485function wp_sprintf_l($pattern, $args) {
2486 // Not a match
2487 if ( substr($pattern, 0, 2) != '%l' )
2488 return $pattern;
2489
2490 // Nothing to work with
2491 if ( empty($args) )
2492 return '';
2493
2494 // Translate and filter the delimiter set (avoid ampersands and entities here)
2495 $l = apply_filters('wp_sprintf_l', array(
2496 /* translators: used between list items, there is a space after the coma */
2497 'between' => __(', '),
2498 /* translators: used between list items, there is a space after the and */
2499 'between_last_two' => __(', and '),
2500 /* translators: used between only two list items, there is a space after the and */
2501 'between_only_two' => __(' and '),
2502 ));
2503
2504 $args = (array) $args;
2505 $result = array_shift($args);
2506 if ( count($args) == 1 )
2507 $result .= $l['between_only_two'] . array_shift($args);
2508 // Loop when more than two args
2509 $i = count($args);
2510 while ( $i ) {
2511 $arg = array_shift($args);
2512 $i--;
2513 if ( 0 == $i )
2514 $result .= $l['between_last_two'] . $arg;
2515 else
2516 $result .= $l['between'] . $arg;
2517 }
2518 return $result . substr($pattern, 2);
2519}
2520
2521/**
2522 * Safely extracts not more than the first $count characters from html string.
2523 *
2524 * UTF-8, tags and entities safe prefix extraction. Entities inside will *NOT*
2525 * be counted as one character. For example &amp; will be counted as 4, &lt; as
2526 * 3, etc.
2527 *
2528 * @since 2.5.0
2529 *
2530 * @param integer $str String to get the excerpt from.
2531 * @param integer $count Maximum number of characters to take.
2532 * @return string The excerpt.
2533 */
2534function wp_html_excerpt( $str, $count ) {
2535 $str = strip_tags( $str );
2536 $str = mb_substr( $str, 0, $count );
2537 // remove part of an entity at the end
2538 $str = preg_replace( '/&[^;\s]{0,6}$/', '', $str );
2539 return $str;
2540}
2541
2542/**
2543 * Add a Base url to relative links in passed content.
2544 *
2545 * By default it supports the 'src' and 'href' attributes. However this can be
2546 * changed via the 3rd param.
2547 *
2548 * @since 2.7.0
2549 *
2550 * @param string $content String to search for links in.
2551 * @param string $base The base URL to prefix to links.
2552 * @param array $attrs The attributes which should be processed.
2553 * @return string The processed content.
2554 */
2555function links_add_base_url( $content, $base, $attrs = array('src', 'href') ) {
2556 $attrs = implode('|', (array)$attrs);
2557 return preg_replace_callback("!($attrs)=(['\"])(.+?)\\2!i",
2558 create_function('$m', 'return _links_add_base($m, "' . $base . '");'),
2559 $content);
2560}
2561
2562/**
2563 * Callback to add a base url to relative links in passed content.
2564 *
2565 * @since 2.7.0
2566 * @access private
2567 *
2568 * @param string $m The matched link.
2569 * @param string $base The base URL to prefix to links.
2570 * @return string The processed link.
2571 */
2572function _links_add_base($m, $base) {
2573 //1 = attribute name 2 = quotation mark 3 = URL
2574 return $m[1] . '=' . $m[2] .
2575 (strpos($m[3], 'http://') === false ?
2576 path_join($base, $m[3]) :
2577 $m[3])
2578 . $m[2];
2579}
2580
2581/**
2582 * Adds a Target attribute to all links in passed content.
2583 *
2584 * This function by default only applies to <a> tags, however this can be
2585 * modified by the 3rd param.
2586 *
2587 * <b>NOTE:</b> Any current target attributed will be striped and replaced.
2588 *
2589 * @since 2.7.0
2590 *
2591 * @param string $content String to search for links in.
2592 * @param string $target The Target to add to the links.
2593 * @param array $tags An array of tags to apply to.
2594 * @return string The processed content.
2595 */
2596function links_add_target( $content, $target = '_blank', $tags = array('a') ) {
2597 $tags = implode('|', (array)$tags);
2598 return preg_replace_callback("!<($tags)(.+?)>!i",
2599 create_function('$m', 'return _links_add_target($m, "' . $target . '");'),
2600 $content);
2601}
2602/**
2603 * Callback to add a target attribute to all links in passed content.
2604 *
2605 * @since 2.7.0
2606 * @access private
2607 *
2608 * @param string $m The matched link.
2609 * @param string $target The Target to add to the links.
2610 * @return string The processed link.
2611 */
2612function _links_add_target( $m, $target ) {
2613 $tag = $m[1];
2614 $link = preg_replace('|(target=[\'"](.*?)[\'"])|i', '', $m[2]);
2615 return '<' . $tag . $link . ' target="' . $target . '">';
2616}
2617
2618// normalize EOL characters and strip duplicate whitespace
2619function normalize_whitespace( $str ) {
2620 $str = trim($str);
2621 $str = str_replace("\r", "\n", $str);
2622 $str = preg_replace( array( '/\n+/', '/[ \t]+/' ), array( "\n", ' ' ), $str );
2623 return $str;
2624}
2625
2626?>