Digg Style URL String Parser

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def urlsafestring(string):
	from string import strip, rstrip, lstrip, replace
	string = str(string)
	badchars = [".",",","+","=","%","#","!","@","$","^","*","(",")","\\","/","\"","[","]","{","}","|","'","?"]
	for b in badchars:
		string = replace(string, b, "")
	string = rstrip(string)
	string = lstrip(string)
	string = replace(string, "-", "_")
	string = replace(string, " ", "_")
	string = replace(string, "__", "_")
	return string

Comments

derivin (on March 26, 2007):

this might be better:

import re
match_non_alnum = re.compile("[^a-zA-Z0-9]+")

def dig_url_encode(string):
    return match_non_alnum.sub(' ', string).strip().replace(' ', '_')

Example:

>>> dig_url_encode('.hi\'s., is (a) $ [test], will it "work"/ \\')
'hi_s_is_a_test_will_it_work'
>>>

#

robbie (on March 30, 2007):

Isn't this essentially the same as Django's slugify filter?

#

(Forgotten your password?)

You may use Markdown syntax here, but raw HTML will be removed.