<?php
function wptexturize($text) {
+ global $wp_cockneyreplace;
$output = '';
// Capture tags and everything inside them
$textarr = preg_split("/(<.*>)/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
$curl = str_replace('...', '…', $curl);
$curl = str_replace('``', '“', $curl);
- // This is a hack, look at this more later. It works pretty well though.
- $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");
- $cockneyreplace = array("’tain’t","’twere","’twas","’tis","’twill","’til","’bout","’nuff","’round","’cause");
+ // if a plugin has provided an autocorrect array, use it
+ if ( isset($wp_cockneyreplace) ) {
+ $cockney = array_keys($wp_cockneyreplace);
+ $cockney_replace = array_values($wp_cockneyreplace);
+ } else {
+ $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");
+ $cockneyreplace = array("’tain’t","’twere","’twas","’tis","’twill","’til","’bout","’nuff","’round","’cause");
+ }
+
$curl = str_replace($cockney, $cockneyreplace, $curl);
$curl = preg_replace("/'s/", '’s', $curl);
} else {
$next = true;
}
- $curl = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/', '&$1', $curl);
+ $curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&$1', $curl);
$output .= $curl;
}
return $output;
$pee = preg_replace('!(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)\s*</p>!', "$1", $pee);
if ($br) $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
$pee = preg_replace('!(</?(?:table|thead|tfoot|caption|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)\s*<br />!', "$1", $pee);
- $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)!', '$1', $pee);
+ $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
$pee = preg_replace('!(<pre.*?>)(.*?)</pre>!ise', " stripslashes('$1') . stripslashes(clean_pre('$2')) . '</pre>' ", $pee);
return $pee;
function wp_specialchars( $text, $quotes = 0 ) {
// Like htmlspecialchars except don't double-encode HTML entities
- $text = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/', '&$1', $text);-
+ $text = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/', '&$1', $text);
$text = str_replace('<', '<', $text);
$text = str_replace('>', '>', $text);
- if ( $quotes ) {
+ if ( 'double' === $quotes ) {
+ $text = str_replace('"', '"', $text);
+ } elseif ( 'single' === $quotes ) {
+ $text = str_replace("'", ''', $text);
+ } elseif ( $quotes ) {
$text = str_replace('"', '"', $text);
$text = str_replace("'", ''', $text);
}
}
function remove_accents($string) {
+ if ( !preg_match('/[\x80-\xff]/', $string) )
+ return $string;
+
if (seems_utf8($string)) {
$chars = array(
// Decompositions for Latin-1 Supplement
chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
// Euro Sign
- chr(226).chr(130).chr(172) => 'E');
-
+ chr(226).chr(130).chr(172) => 'E',
+ // GBP (Pound) Sign
+ chr(194).chr(163) => '');
+
$string = strtr($string, $chars);
} else {
// Assume ISO-8859-1 if not UTF-8
// If strict, reduce to ASCII for max portability.
if ( $strict )
- $username = preg_replace('|[^a-z0-9 _.-@]|i', '', $username);
+ $username = preg_replace('|[^a-z0-9 _.\-@]|i', '', $username);
return apply_filters('sanitize_user', $username, $raw_username, $strict);
}
/*
balanceTags
-
+
Balances Tags of string using a modified stack.
-
+
@param text Text to be balanced
+ @param force Forces balancing, ignoring the value of the option
@return Returns balanced text
@author Leonard Lin (leonard@acm.org)
@version v1.1
@date November 4, 2001
@license GPL v2.0
- @notes
- @changelog
+ @notes
+ @changelog
--- Modified by Scott Reilly (coffee2code) 02 Aug 2004
- 1.2 ***TODO*** Make better - change loop condition to $text
- 1.1 Fixed handling of append/stack pop order of end text
- Added Cleaning Hooks
- 1.0 First Version
+ 1.2 ***TODO*** Make better - change loop condition to $text
+ 1.1 Fixed handling of append/stack pop order of end text
+ Added Cleaning Hooks
+ 1.0 First Version
*/
-function balanceTags($text, $is_comment = 0) {
-
- if ( get_option('use_balanceTags') == 0)
+function balanceTags($text, $force = false) {
+
+ if ( !$force && get_option('use_balanceTags') == 0 )
return $text;
$tagstack = array(); $stacksize = 0; $tagqueue = ''; $newtext = '';
if ($regex[1][0] == "/") { // End Tag
$tag = strtolower(substr($regex[1],1));
// if too many closing tags
- if($stacksize <= 0) {
+ if($stacksize <= 0) {
$tag = '';
//or close to be safe $tag = '/' . $tag;
}
}
$newtext .= substr($text,0,$i) . $tag;
$text = substr($text,$i+$l);
- }
+ }
// Clear Tag Queue
$newtext .= $tagqueue;
return $newtext;
}
+function force_balance_tags($text) {
+ return balanceTags($text, true);
+}
function format_to_edit($content, $richedit = false) {
$content = apply_filters('format_to_edit', $content);
function zeroise($number,$threshold) { // function to add leading zeros when necessary
return sprintf('%0'.$threshold.'s', $number);
- }
+}
function backslashit($string) {
+ $string = preg_replace('/^([0-9])/', '\\\\\\\\\1', $string);
$string = preg_replace('/([a-z])/i', '\\\\\1', $string);
return $string;
}
}
function make_clickable($ret) {
- $ret = ' ' . $ret . ' ';
- $ret = preg_replace("#([\s>])(https?)://([^\s<>{}()]+[^\s.,<>{}()])#i", "$1<a href='$2://$3' rel='nofollow'>$2://$3</a>", $ret);
- $ret = preg_replace("#(\s)www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^ <>{}()\n\r]*[^., <>{}()\n\r]?)?)#i", "$1<a href='http://www.$2.$3$4' rel='nofollow'>www.$2.$3$4</a>", $ret);
- $ret = preg_replace("#(\s)([a-z0-9\-_.]+)@([a-z0-9\-_.]+)\.([^,< \n\r]+)#i", "$1<a href=\"mailto:$2@$3.$4\">$2@$3.$4</a>", $ret);
+ $ret = ' ' . $ret;
+ // in testing, using arrays here was found to be faster
+ $ret = preg_replace(
+ array(
+ '#([\s>])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is',
+ '#([\s>])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is',
+ '#([\s>])([a-z0-9\-_.]+)@([^,< \n\r]+)#i'),
+ array(
+ '$1<a href="$2" rel="nofollow">$2</a>',
+ '$1<a href="http://$2" rel="nofollow">$2</a>',
+ '$1<a href="mailto:$2@$3">$2@$3</a>'),$ret);
+ // this one is not in an array because we need it to run last, for cleanup of accidental links within links
+ $ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);
$ret = trim($ret);
return $ret;
}
function wp_rel_nofollow( $text ) {
- $text = preg_replace('|<a (.+?)>|i', '<a $1 rel="nofollow">', $text);
+ $text = preg_replace('|<a (.+?)>|ie', "'<a ' . str_replace(' rel=\"nofollow\"','',stripslashes('$1')) . ' rel=\"nofollow\">'", $text);
return $text;
}
'♦' => '♦'
);
- foreach ($to_ncr as $entity => $ncr) {
- $text = str_replace($entity, $ncr, $text);
- }
- return $text;
+ return str_replace( array_keys($to_ncr), array_values($to_ncr), $text );
}
function wp_richedit_pre($text) {
return apply_filters('richedit_pre', $output);
}
+function clean_url( $url, $protocols = null ) {
+ if ('' == $url) return $url;
+ $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%]|i', '', $url);
+ $strip = array('%0d', '%0a');
+ $url = str_replace($strip, '', $url);
+ $url = str_replace(';//', '://', $url);
+ // Append http unless a relative link starting with / or a php file.
+ if ( strpos($url, '://') === false &&
+ substr( $url, 0, 1 ) != '/' && !preg_match('/^[a-z0-9]+?\.php/i', $url) )
+ $url = 'http://' . $url;
+
+ $url = preg_replace('/&([^#])(?![a-z]{2,8};)/', '&$1', $url);
+ if ( !is_array($protocols) )
+ $protocols = array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet');
+ if ( wp_kses_bad_protocol( $url, $protocols ) != $url )
+ return '';
+ return $url;
+}
+
+// Escape single quotes, specialchar double quotes, and fix line endings.
+function js_escape($text) {
+ $safe_text = wp_specialchars($text, 'double');
+ $safe_text = preg_replace('/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes($safe_text));
+ $safe_text = preg_replace("/\r?\n/", "\\n", addslashes($safe_text));
+ return apply_filters('js_escape', $safe_text, $text);
+}
+
+// Escaping for HTML attributes
+function attribute_escape($text) {
+ $safe_text = wp_specialchars($text, true);
+ return apply_filters('attribute_escape', $safe_text, $text);
+}
+
?>