Login

Digg Style URL String Parser

Author:
addicted
Posted:
March 26, 2007
Language:
Python
Version:
.96
Tags:
url clean simatic
Score:
-2 (after 2 ratings)

Does a digg url effect to a string, can be useful for using an item's title in the url,

from this:

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

to this:

his_is_a_test_will_it_work

I understand this isn't a very well made script, I am not very good at string manipulation. But I would be happy if someone would recode it in a faster, more managable way. I recomend saving the rendering.

 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 2 years, 5 months ago
  2. Modify query string on a url by dnordberg 7 years ago
  3. EncryptField by volksman 6 years, 9 months ago
  4. Default URL handler by solartic 4 years, 2 months ago
  5. yet another digg style paginator by akonsu 5 years, 8 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?

#

Please login first before commenting.