Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Remove special characters from string
Started by contreras2004 Jan 06 2014 12:04 PM

- - - - -
3 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

contreras2004

[GLOBAL: userInfoPane.html]
contreras2004
  • Observer

  • 16 posts
  • Corona SDK

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,



[TOPIC: post.html]
#2

nmichaud

[GLOBAL: userInfoPane.html]
nmichaud
  • Contributor

  • 492 posts
  • Corona SDK

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


[TOPIC: post.html]
#3

contreras2004

[GLOBAL: userInfoPane.html]
contreras2004
  • Observer

  • 16 posts
  • Corona SDK

Many many thanks.

It worked like charm  :)



[TOPIC: post.html]
#4

contreras2004

[GLOBAL: userInfoPane.html]
contreras2004
  • Observer

  • 16 posts
  • Corona SDK

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.

 

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
 
 
It even works with Chinese addresses .
I hope someone find it useful.
 
;)



[topic_controls]
[/topic_controls]