Show
Ignore:
Timestamp:
05/05/2008 12:56:02 PM (7 months ago)
Author:
Geert
Message:

Optimized text::auto_p() by automatically skipping regexes that are only needed if the string contains existing html. Since you'll often pass strings that have gone through html::specialchars(), the potential of saving time in text::auto_p() is rather high.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/helpers/text.php

    r2404 r2635  
    299299                $str = preg_replace('~[ \t]+$~m', '', $str); 
    300300 
    301                 // Elements that should not be surrounded by p tags 
    302                 $no_p = '(?:p|div|h[1-6r]|ul|ol|li|blockquote|d[dlt]|pre|t[dhr]|t(?:able|body|foot|head)|c(?:aption|olgroup)|form|s(?:elect|tyle)|a(?:ddress|rea)|ma(?:p|th))'; 
    303  
    304                 // Put at least two linebreaks before and after $no_p elements 
    305                 $str = preg_replace('~^<'.$no_p.'[^>]*+>~im', "\n$0", $str); 
    306                 $str = preg_replace('~</'.$no_p.'\s*+>$~im', "$0\n", $str); 
     301                // The following regexes only need to be executed if the string contains html 
     302                if (($html_found = strpos($str, '<')) !== FALSE) 
     303                { 
     304                        // Elements that should not be surrounded by p tags 
     305                        $no_p = '(?:p|div|h[1-6r]|ul|ol|li|blockquote|d[dlt]|pre|t[dhr]|t(?:able|body|foot|head)|c(?:aption|olgroup)|form|s(?:elect|tyle)|a(?:ddress|rea)|ma(?:p|th))'; 
     306 
     307                        // Put at least two linebreaks before and after $no_p elements 
     308                        $str = preg_replace('~^<'.$no_p.'[^>]*+>~im', "\n$0", $str); 
     309                        $str = preg_replace('~</'.$no_p.'\s*+>$~im', "$0\n", $str); 
     310                } 
    307311 
    308312                // Do the <p> magic! 
     
    310314                $str = preg_replace('~\n{2,}~', "</p>\n\n<p>", $str); 
    311315 
    312                 // Remove p tags around $no_p elements 
    313                 $str = preg_replace('~<p>(?=</?'.$no_p.'[^>]*+>)~i', '', $str); 
    314                 $str = preg_replace('~(</?'.$no_p.'[^>]*+>)</p>~i', '$1', $str); 
     316                // The following regexes only need to be executed if the string contains html 
     317                if ($html_found !== FALSE) 
     318                { 
     319                        // Remove p tags around $no_p elements 
     320                        $str = preg_replace('~<p>(?=</?'.$no_p.'[^>]*+>)~i', '', $str); 
     321                        $str = preg_replace('~(</?'.$no_p.'[^>]*+>)</p>~i', '$1', $str); 
     322                } 
    315323 
    316324                // Convert single linebreaks to <br />