Make WordPress Core

Changeset 4511


Ignore:
Timestamp:
11/21/2006 10:00:10 PM (18 years ago)
Author:
ryan
Message:

Make wptexturize faster. Props ecb29. fixes #2980

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/formatting.php

    r4495 r4511  
    33function wptexturize($text) {
    44    global $wp_cockneyreplace;
     5    $next = true;
    56    $output = '';
    6     // Capture tags and everything inside them
    7     $textarr = preg_split("/(<.*>)/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
    8     $stop = count($textarr); $next = true; // loop stuff
    9     for ($i = 0; $i < $stop; $i++) {
    10         $curl = $textarr[$i];
     7    $curl = '';
     8    $textarr = preg_split('/(<.*>)/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
     9    $stop = count($textarr);
     10
     11    // if a plugin has provided an autocorrect array, use it
     12    if ( isset($wp_cockneyreplace) ) {
     13        $cockney = array_keys($wp_cockneyreplace);
     14        $cockney_replace = array_values($wp_cockneyreplace);
     15    } else {
     16        $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");
     17        $cockneyreplace = array("&#8217;tain&#8217;t","&#8217;twere","&#8217;twas","&#8217;tis","&#8217;twill","&#8217;til","&#8217;bout","&#8217;nuff","&#8217;round","&#8217;cause");
     18    }
     19
     20    $static_characters = array_merge(array('---', ' -- ', '--', 'xn&#8211;', '...', '``', '\'s', '\'\'', ' (tm)'), $cockney);
     21    $static_replacements = array_merge(array('&#8212;', ' &#8212; ', '&#8211;', 'xn--', '&#8230;', '&#8220;', '&#8217;s', '&#8221;', ' &#8482;'), $cockneyreplace);
     22
     23    $dynamic_characters = array('/\'(\d\d(?:&#8217;|\')?s)/', '/(\s|\A|")\'/', '/(\d+)"/', '/(\d+)\'/', '/(\S)\'([^\'\s])/', '/(\s|\A)"(?!\s)/', '/"(\s|\S|\Z)/', '/\'([\s.]|\Z)/', '/(\d+)x(\d+)/');
     24    $dynamic_replacements = array('&#8217;$1','$1&#8216;', '$1&#8243;', '$1&#8242;', '$1&#8217;$2', '$1&#8220;$2', '&#8221;$1', '&#8217;$1', '$1&#215;$2');
     25
     26    for ( $i = 0; $i < $stop; $i++ ) {
     27        $curl = $textarr[$i];
    1128
    1229        if (isset($curl{0}) && '<' != $curl{0} && $next) { // If it's not a tag
    13             $curl = str_replace('---', '&#8212;', $curl);
    14             $curl = str_replace(' -- ', ' &#8212; ', $curl);
    15             $curl = str_replace('--', '&#8211;', $curl);
    16             $curl = str_replace('xn&#8211;', 'xn--', $curl);
    17             $curl = str_replace('...', '&#8230;', $curl);
    18             $curl = str_replace('``', '&#8220;', $curl);
    19 
    20             // if a plugin has provided an autocorrect array, use it
    21             if ( isset($wp_cockneyreplace) ) {
    22                 $cockney = array_keys($wp_cockneyreplace);
    23                 $cockney_replace = array_values($wp_cockneyreplace);
    24             } else {
    25                 $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");
    26                 $cockneyreplace = array("&#8217;tain&#8217;t","&#8217;twere","&#8217;twas","&#8217;tis","&#8217;twill","&#8217;til","&#8217;bout","&#8217;nuff","&#8217;round","&#8217;cause");
    27             }
    28 
    29             $curl = str_replace($cockney, $cockneyreplace, $curl);
    30 
    31             $curl = preg_replace("/'s/", '&#8217;s', $curl);
    32             $curl = preg_replace("/'(\d\d(?:&#8217;|')?s)/", "&#8217;$1", $curl);
    33             $curl = preg_replace('/(\s|\A|")\'/', '$1&#8216;', $curl);
    34             $curl = preg_replace('/(\d+)"/', '$1&#8243;', $curl);
    35             $curl = preg_replace("/(\d+)'/", '$1&#8242;', $curl);
    36             $curl = preg_replace("/(\S)'([^'\s])/", "$1&#8217;$2", $curl);
    37             $curl = preg_replace('/(\s|\A)"(?!\s)/', '$1&#8220;$2', $curl);
    38             $curl = preg_replace('/"(\s|\S|\Z)/', '&#8221;$1', $curl);
    39             $curl = preg_replace("/'([\s.]|\Z)/", '&#8217;$1', $curl);
    40             $curl = preg_replace("/ \(tm\)/i", ' &#8482;', $curl);
    41             $curl = str_replace("''", '&#8221;', $curl);
    42 
    43             $curl = preg_replace('/(\d+)x(\d+)/', "$1&#215;$2", $curl);
    44 
     30            // static strings
     31            $curl = str_replace($static_characters, $static_replacements, $curl);
     32
     33            // regular expressions
     34            $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
    4535        } elseif (strstr($curl, '<code') || strstr($curl, '<pre') || strstr($curl, '<kbd' || strstr($curl, '<style') || strstr($curl, '<script'))) {
    46             // strstr is fast
    4736            $next = false;
    4837        } else {
    4938            $next = true;
    5039        }
     40
    5141        $curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&#038;$1', $curl);
    5242        $output .= $curl;
    5343    }
    54     return $output;
     44
     45    return $output;
    5546}
    5647
Note: See TracChangeset for help on using the changeset viewer.