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

More like this

  1. Filter that adds a period at the end of a string by Ztyx 1 year, 3 months ago
  2. Modify query string on a url by dnordberg 5 years, 9 months ago
  3. EncryptField by volksman 5 years, 7 months ago
  4. Default URL handler by solartic 3 years ago
  5. yet another digg style paginator by akonsu 4 years, 6 months ago

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?)