Part of the EllisLab Network
   
 
[OPTIMIZATION] URL Helper > url_title()
Posted: 14 May 2007 09:02 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  248
Joined  02-10-2007

system/helpers/url_helper.php - line 397


BEFORE: 10000 iterations take about 0.6535 seconds

function url_title($str, $separator = 'dash')
{
    
if ($separator == 'dash')
    
{
        $search        
= '_';
        
$replace    = '-';
    
}
    
else
    
{
        $search        
= '-';
        
$replace    = '_';
    
}
        
    $trans
= array(
                    
$search                                => $replace,
                    
"\s+"                                => $replace,
                    
"[^a-z0-9".$replace."]"                => '',
                    
$replace."+"                        => $replace,
                    
$replace."$"                        => '',
                    
"^".$replace                        => ''
                   
);

    
$str = strip_tags(strtolower($str));
    
    foreach (
$trans as $key => $val)
    
{
        $str
= preg_replace("#".$key."#", $val, $str);
    
}
    
    
return trim(stripslashes($str));
}


AFTER: 10000 iterations take about 0.3301 seconds

function url_title_geert($str, $separator = 'dash') {
    
    
// Would you like dashes or underscores, sir?
    
$separator = ($separator == 'dash') ? '-' : '_';
    
    
// Let's prepare our string.
    
$str = trim($str, " -_\t\n\r");
    
$str = strtolower($str);
    
$str = strip_tags($str);
    
    
// Time for the hard work now, just two regexes.
    
$str = preg_replace('/[-_\s]+/', $separator, $str);
    
$str = preg_replace('/[^a-z0-9'. $separator .']/', '', $str);
    
    
// All done.
    
return $str;
}

Twice as fast. Woot!

 Signature 

Kohana rocks!

Profile
 
 
Posted: 14 May 2007 11:10 AM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  956
Joined  08-06-2006

I gave this a good long look and I like it. Much easier to read too.

 Signature 

imap_pop_class - get email and attachments site_migrate - port sites to CI OOCalendar - OO Calendar

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 77482 Total Logged-in Users: 21
Total Topics: 101518 Total Anonymous Users: 2
Total Replies: 544257 Total Guests: 186
Total Posts: 645775    
Members ( View Memberlist )