Remove special characters from string

Hi, 

Im using Google api for keyword searching and everything workd fine untill the user types a keyword with a special character. For example: “Cristóbal Colón”

I need to make the string “Cristóbal Colón” look like “Cristobal+Colon” so I can send it through the URL so I need to take away every special character from the user input.

Is there any function to do this easily or should I make it manually? 

Best regards,

Hi,

Here is a method we are using internally to solve the same problem as you.

local tableAccents = {}

    tableAccents[“à”] = “a”

    tableAccents[“á”] = “a”

    tableAccents[“â”] = “a”

    tableAccents[“ã”] = “a”

    tableAccents[“ä”] = “a”

    tableAccents[“ç”] = “c”

    tableAccents[“è”] = “e”

    tableAccents[“é”] = “e”

    tableAccents[“ê”] = “e”

    tableAccents[“ë”] = “e”

    tableAccents[“ì”] = “i”

    tableAccents[“í”] = “i”

    tableAccents[“î”] = “i”

    tableAccents[“ï”] = “i”

    tableAccents[“ñ”] = “n”

    tableAccents[“ò”] = “o”

    tableAccents[“ó”] = “o”

    tableAccents[“ô”] = “o”

    tableAccents[“õ”] = “o”

    tableAccents[“ö”] = “o”

    tableAccents[“ù”] = “u”

    tableAccents[“ú”] = “u”

    tableAccents[“û”] = “u”

    tableAccents[“ü”] = “u”

    tableAccents[“ý”] = “y”

    tableAccents[“ÿ”] = “y”

    tableAccents[“À”] = “A”

    tableAccents[“Á”] = “A”

    tableAccents[“”] = “A”

    tableAccents[“Ô] = “A”

    tableAccents[“Ä”] = “A”

    tableAccents[“Ç”] = “C”

    tableAccents[“È”] = “E”

    tableAccents[“É”] = “E”

    tableAccents[“Ê”] = “E”

    tableAccents[“Ë”] = “E”

    tableAccents[“Ì”] = “I”

    tableAccents[“Í”] = “I”

    tableAccents[“Δ] = “I”

    tableAccents[“Ï”] = “I”

    tableAccents[“Ñ”] = “N”

    tableAccents[“Ò”] = “O”

    tableAccents[“Ó”] = “O”

    tableAccents[“Ô”] = “O”

    tableAccents[“Õ”] = “O”

    tableAccents[“Ö”] = “O”

    tableAccents[“Ù”] = “U”

    tableAccents[“Ú”] = “U”

    tableAccents[“Û”] = “U”

    tableAccents[“Ü”] = “U”

    tableAccents[“Ý”] = “Y”

– Strip accents from a string

function string.stripAccents( str )

        

    local normalizedString = “”

    for strChar in string.gfind(str, “([%z\1-\127\194-\244][\128-\191]*)”) do

        if tableAccents[strChar] ~= nil then

            normalizedString = normalizedString…tableAccents[strChar]

        else

            normalizedString = normalizedString…strChar

        end

    end

        

  return normalizedString

end

Many many thanks.

It worked like charm  :slight_smile:

Hey, just if someone is wondering. I found another way to do this. 

This function encodes the URL perfectly and Google api is able to get the request perfectly, you just pass the search string and then you can add it to the complete adrdess.

[lua]function urlencode(str)
if (str) then
str = string.gsub (str, “\n”, “\r\n”)
str = string.gsub (str, “([^%w])”,
function © return string.format ("%%%02X", string.byte©) end)
str = string.gsub (str, " ", “+”)
end
return str
end[/lua]

It even works with Chinese addresses .

I hope someone find it useful.

:wink:

Hi,

Here is a method we are using internally to solve the same problem as you.

local tableAccents = {}

    tableAccents[“à”] = “a”

    tableAccents[“á”] = “a”

    tableAccents[“â”] = “a”

    tableAccents[“ã”] = “a”

    tableAccents[“ä”] = “a”

    tableAccents[“ç”] = “c”

    tableAccents[“è”] = “e”

    tableAccents[“é”] = “e”

    tableAccents[“ê”] = “e”

    tableAccents[“ë”] = “e”

    tableAccents[“ì”] = “i”

    tableAccents[“í”] = “i”

    tableAccents[“î”] = “i”

    tableAccents[“ï”] = “i”

    tableAccents[“ñ”] = “n”

    tableAccents[“ò”] = “o”

    tableAccents[“ó”] = “o”

    tableAccents[“ô”] = “o”

    tableAccents[“õ”] = “o”

    tableAccents[“ö”] = “o”

    tableAccents[“ù”] = “u”

    tableAccents[“ú”] = “u”

    tableAccents[“û”] = “u”

    tableAccents[“ü”] = “u”

    tableAccents[“ý”] = “y”

    tableAccents[“ÿ”] = “y”

    tableAccents[“À”] = “A”

    tableAccents[“Á”] = “A”

    tableAccents[“”] = “A”

    tableAccents[“Ô] = “A”

    tableAccents[“Ä”] = “A”

    tableAccents[“Ç”] = “C”

    tableAccents[“È”] = “E”

    tableAccents[“É”] = “E”

    tableAccents[“Ê”] = “E”

    tableAccents[“Ë”] = “E”

    tableAccents[“Ì”] = “I”

    tableAccents[“Í”] = “I”

    tableAccents[“Δ] = “I”

    tableAccents[“Ï”] = “I”

    tableAccents[“Ñ”] = “N”

    tableAccents[“Ò”] = “O”

    tableAccents[“Ó”] = “O”

    tableAccents[“Ô”] = “O”

    tableAccents[“Õ”] = “O”

    tableAccents[“Ö”] = “O”

    tableAccents[“Ù”] = “U”

    tableAccents[“Ú”] = “U”

    tableAccents[“Û”] = “U”

    tableAccents[“Ü”] = “U”

    tableAccents[“Ý”] = “Y”

– Strip accents from a string

function string.stripAccents( str )

        

    local normalizedString = “”

    for strChar in string.gfind(str, “([%z\1-\127\194-\244][\128-\191]*)”) do

        if tableAccents[strChar] ~= nil then

            normalizedString = normalizedString…tableAccents[strChar]

        else

            normalizedString = normalizedString…strChar

        end

    end

        

  return normalizedString

end

Many many thanks.

It worked like charm  :slight_smile:

Hey, just if someone is wondering. I found another way to do this. 

This function encodes the URL perfectly and Google api is able to get the request perfectly, you just pass the search string and then you can add it to the complete adrdess.

[lua]function urlencode(str)
if (str) then
str = string.gsub (str, “\n”, “\r\n”)
str = string.gsub (str, “([^%w])”,
function © return string.format ("%%%02X", string.byte©) end)
str = string.gsub (str, " ", “+”)
end
return str
end[/lua]

It even works with Chinese addresses .

I hope someone find it useful.

:wink: