#2980 closed enhancement (fixed)
Improvements to wptexturize
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | 2.1 | Priority: | normal |
| Severity: | normal | Version: | 2.0.3 |
| Component: | Optimization | Keywords: | wptexturize optimize |
| Focuses: | Cc: |
Description
The wptexturize function in functions-formatting.php can be significantly improved by some simple refactoring. My measurements show this reduces the time spent inside wptexturize from 24% to 16% of total wp(), and the time of the function itself from 600ms to 200ms. This also reduces the number of preg_replace calls dramatically, from 10,439 to 3,289 and total time from 74ms to 36ms. Also, we’ve gone from 54ms of 6,410 str_replace calls to 29ms of 1,405 calls.
function wptexturize($text) {
$next = true;
$output = '';
$curl = '';
$textarr = preg_split('/(<.*>)/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
$stop = count($textarr);
for($i = 0; $i < $stop; $i++){
$curl = $textarr[$i];
if (isset($curl{0}) && '<' != $curl{0} && $next) { // If it's not a tag
// static strings
$static_characters = array('—', ' — ', '–', 'xn--', '…', '“', '\'tain\'t', '\'twere', '\'twas', '\'tis', '\'twill', '\'til', '\'bout', '\'nuff', '\'round', '\'cause', '\'s', '\'\'', ' (tm)');
$static_replacements = array('---', ' -- ', '--', 'xn–', '...', '``', '’tain’t', '’twere', '’twas', '’tis', '’twill', '’til', '’bout', '’nuff', '’round', '’cause', '’s', '”', ' ™');
$curl = str_replace($static_characters, $static_replacements, $curl);
// regular expressions
$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');
$curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
} elseif (strstr($curl, '<code') || strstr($curl, '<pre') || strstr($curl, '<kbd' || strstr($curl, '<style') || strstr($curl, '<script'))) {
// strstr is fast
$next = false;
} else {
$next = true;
}
$curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&$1', $curl);
$output .= $curl;
}
return $output;
}
I'll try and attach a patch...
Attachments (1)
Change History (8)
#2
@
19 years ago
Looks good offhand. We'll need to test this well to make sure we don't break anything. Since texturize is run on display rather than on save, we can easily do side-by-side comparisons. Load a a page using the old version and save page source. Load the same page using the new version and save source. Diff the two page sources.
Patch diff