I would like to sanitize a string in to a URL so this is what I basically need.
- Everything must be removed except alphanumeric characters and spaces and dashed.
- Spaces should be converter into dashes.
Eg.
This, is the URL!
must return
this-is-the-url
All previous asnwers deal with url, but in case some one will need to sanitize string for login (e.g.) and keep it as text, here is you go:
First strip unwanted characters
Then changes spaces for unserscores
Finally encode it ready for use
This will do it in a Unix shell (I just tried it on my MacOS):
I got the idea from a blog post on More Shell, Less Egg
Try This
Usage:
Will output:
abcdef-g
source : https://stackoverflow.com/a/14114419/2439715