From 6f8d9c92a5b673d903e519b2ff9e3c9f32bb4590 Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 21 Nov 2006 22:00:10 +0000 Subject: [PATCH] Make wptexturize faster. Props ecb29. fixes #2980 git-svn-id: http://svn.automattic.com/wordpress/trunk@4511 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/formatting.php | 67 +++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 38 deletions(-) diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php index 94d2c5884..f20d4864c 100644 --- a/wp-includes/formatting.php +++ b/wp-includes/formatting.php @@ -2,56 +2,47 @@ function wptexturize($text) { global $wp_cockneyreplace; + $next = true; $output = ''; - // Capture tags and everything inside them - $textarr = preg_split("/(<.*>)/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE); - $stop = count($textarr); $next = true; // loop stuff - for ($i = 0; $i < $stop; $i++) { - $curl = $textarr[$i]; + $curl = ''; + $textarr = preg_split('/(<.*>)/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE); + $stop = count($textarr); + + // 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"); + } + + $static_characters = array_merge(array('---', ' -- ', '--', 'xn–', '...', '``', '\'s', '\'\'', ' (tm)'), $cockney); + $static_replacements = array_merge(array('—', ' — ', '–', 'xn--', '…', '“', '’s', '”', ' ™'), $cockneyreplace); + + $dynamic_characters = array('/\'(\d\d(?:’|\')?s)/', '/(\s|\A|")\'/', '/(\d+)"/', '/(\d+)\'/', '/(\S)\'([^\'\s])/', '/(\s|\A)"(?!\s)/', '/"(\s|\S|\Z)/', '/\'([\s.]|\Z)/', '/(\d+)x(\d+)/'); + $dynamic_replacements = array('’$1','$1‘', '$1″', '$1′', '$1’$2', '$1“$2', '”$1', '’$1', '$1×$2'); + + for ( $i = 0; $i < $stop; $i++ ) { + $curl = $textarr[$i]; if (isset($curl{0}) && '<' != $curl{0} && $next) { // If it's not a tag - $curl = str_replace('---', '—', $curl); - $curl = str_replace(' -- ', ' — ', $curl); - $curl = str_replace('--', '–', $curl); - $curl = str_replace('xn–', 'xn--', $curl); - $curl = str_replace('...', '…', $curl); - $curl = str_replace('``', '“', $curl); - - // 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); - $curl = preg_replace("/'(\d\d(?:’|')?s)/", "’$1", $curl); - $curl = preg_replace('/(\s|\A|")\'/', '$1‘', $curl); - $curl = preg_replace('/(\d+)"/', '$1″', $curl); - $curl = preg_replace("/(\d+)'/", '$1′', $curl); - $curl = preg_replace("/(\S)'([^'\s])/", "$1’$2", $curl); - $curl = preg_replace('/(\s|\A)"(?!\s)/', '$1“$2', $curl); - $curl = preg_replace('/"(\s|\S|\Z)/', '”$1', $curl); - $curl = preg_replace("/'([\s.]|\Z)/", '’$1', $curl); - $curl = preg_replace("/ \(tm\)/i", ' ™', $curl); - $curl = str_replace("''", '”', $curl); - - $curl = preg_replace('/(\d+)x(\d+)/', "$1×$2", $curl); + // static strings + $curl = str_replace($static_characters, $static_replacements, $curl); + // regular expressions + $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl); } elseif (strstr($curl, '