views:

123

answers:

4

Hello, I'm trying to create a simple method to turn a name (first name, last name, middle initial) into a public URL-friendly ID (like Stackoverflow does with question titles). Now people could enter all kinds of crazy characters, umlauts etc., is there something in .NET I can use to normalize it to URL-acceptable/english characters or do I need to write my own method to get this done?

Thank you!

Edit: An example (e.g. via RegEx or other way) would be super helpful!!! :)

A: 

A regular expression to validate the string with the characters and lengths you wish to allow.

Yuriy Faktorovich
A: 

Think you'll have to write your own method...

Safelist of characters...

A to Z

For Each c As char In SafeList If safe ... etc. Next

Paul
+8  A: 

Sounds like what you're after is a Slug Generator!

CraigTP
Yes! Nice link thank you!
Alex
Isn't there a danger when removing characters that you could get the same return value from two different inputs and hence end up with a non-unique identifier?
Dan Diplo
My understanding is that you use a slug with a unique ID number, not in place of. For example, there are two URLs to get to this question - http://stackoverflow.com/questions/1188283/generate-public-id-text-like-stackoverflow and http://stackoverflow.com/questions/1188283/
Thomas Owens
@Dan - You're right, but I'd assume that the input to the "GenerateSlug" function would already have ensured some level of uniqueness that would come from characters that are not going to be removed (ie. numbers etc).
CraigTP
+1  A: 

Simple method using UrlEncode

You obviously have to do something to deal with the collisions (prevent them on user creation being sensible but that means you are tied to this structure)

s => Regex.Replace(HttpUtility.UrlEncode(s), "%..", "")

This is relying on the output of UrlEncode always using two characters for the encoded form and that you are happy to have space convert to '+'

ShuggyCoUk