Changeset 217

Show
Ignore:
Timestamp:
06/16/2007 04:42:35 AM (19 months ago)
Author:
Geert
Message:

url_title() optimization: http://codeigniter.com/forums/viewthread/52307/

Files:
1 modified

Legend:

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

    r216 r217  
    395395 * @return      string 
    396396 */ 
    397 function url_title($str, $separator = 'dash') 
    398 { 
    399         $search  = ($separator == 'dash') ? '_' : '-'; 
    400         $replace = ($separator == 'dash') ? '-' : '_'; 
    401  
    402         $trans = array( 
    403                 $search                 => $replace, 
    404                 '\s+'                   => $replace, 
    405                 '[^a-z0-9'.$replace.']' => '', 
    406                 $replace.'+'            => $replace, 
    407                 $replace.'$'            => '', 
    408                 '^'.$replace            => '' 
    409         ); 
    410  
    411         $str = strip_tags(strtolower($str)); 
    412  
    413         foreach ($trans as $key => $val) 
    414         { 
    415                 $str = preg_replace('#'.$key.'#', $val, $str); 
    416         } 
    417  
    418         return trim(stripslashes($str)); 
     397function url_title($str, $separator = 'dash') { 
     398         
     399        // Dashes or underscores? 
     400        $separator = ($separator == 'dash') ? '-' : '_'; 
     401         
     402        // Let's prepare our string. 
     403        $str = trim($str, " -_\t\n\r"); 
     404        $str = strip_tags($str); 
     405        $str = strtolower($str); 
     406         
     407        // Time for the hard work now, just two regexes. 
     408        $str = preg_replace('/[-_\s]+/', $separator, $str); 
     409        $str = preg_replace('/[^a-z0-9'. $separator .']/', '', $str); 
     410         
     411        return $str; 
    419412} 
    420413