beautiful urls

January 1st, 2009
Tags: python url seo Posted in Google JavaScript :-)Say something

As I am writing posts frequently these days, I feel troublesome to write both the title and slug manually. So I decided to do this via jQuery:

<script type="text/javascript">
jQuery(document).ready(function() {
$("#title").blur(function(){
$("#slug").val( $("#title").val().toLowerCase().replace(/[^\s\w]/g, '').replace(/\s+/g, '-') );
});
});
</script>

So when the slug is used in url, it conforms to the url structure Google Suggested (lowercase, '-'.join(words)). Of course, there are a lot of assumptions to use it: only ASCII characters are used; there won't be extra space in the beginning or the end of the title (a trim() or strip()* should work, though I have to look up what the corresponding name is in JavaScript); there will be space after punctuation characters and so on.

The assumptions are ok except the 1st one - only ASCII character is allowed. Then today I saw a post about using PHP's iconv function to deal with non-ascii :

function toSlug($string,$space="-") {  
     
    if (function_exists('iconv')) { 
        $string = @iconv('UTF-8', 'ASCII//TRANSLIT', $string); 
    } 
 
    $string = preg_replace("/[^a-zA-Z0-9 -]/", "", $string); 
    $string = strtolower($string); 
    $string = str_replace(" ", $space, $string); 
 
    return $string; 
 
}
 

This is not bad. I wish I knew or searched such a Java function or library then I can do much less work for my real job. But again this won't work for Chinese characters, maybe a Google translate can help a bit.

 

strip()*: it's always a fun topic in python, as there may be interesting Google Ads associated with it.

And my github diff for the slug function: http://github.com/xclricky/micolog-cxu/commit/f20bc501068053da9a0ec59a5ecefbb6ae64a93e#diff-1

 

accept trackback»
«Single threaded dev appserver

我要留言

10+8=