user1237870
user1237870

Reputation: 23

sanitize input for ruby

Hallo I would like to sanitize input from Ruby, but simultaneously not have it mess up my strings containing foreign characters.

string1=string.downcase.gsub( /<(.|\n)*?>/, '' ).gsub(" ", "").gsub(",","").gsub("'","").gsub("_", "").gsub(";", "").gsub("-", "").gsub(":","").gsub(".", "").gsub("?", "").gsub("!", "").gsub("^","").gsub("%", "").gsub("$","")

The string needs to be stripped of spaces, apostrophes, and everything but letters (not sure about numbers), in addition to be sanitized. I am not sure if I forgot something, and probably something is redundant.

My code works OK as long as string does not contain harmless non-english characters such as accented letters, which I'd like it to deal with, but they break my code. My guess is that they are converted to %25 and all that stuff and afterwards they break. In fact it breaks even if I don't sanitize at all. How can I tell Ruby to handle non-english characters correctly? Thank you a lot.

Upvotes: 1

Views: 1086

Answers (1)

Selman Ulug
Selman Ulug

Reputation: 867

like this;

" ' ; te st".gsub(/\W+/, "") # "test"

Upvotes: 2

Related Questions