1

I have this loop that decodes the names in array "persons":

for(var idxT in persons){

    var name = persons[idxT]; 

    name = decodeURIComponent(escape(name ));    

    players[idxT] = name ;

} //endFor

It decodes well the first 2 names, but when arrives at the third name (name = "AntÃnio Nunes") logs this on the console:

Uncaught URIError: URI malformed

NOTE: The names in the array where previously encoded in php with "utf8_encode"

The problem is this character "ó". How can i handle this in a way that can decode all the names?

Thanks

  • Why are you calling `escape`? (There's basically never any reason to use that antiquated function.) – T.J. Crowder May 15 '18 at 16:43
  • Without the "escape" the string is not decoded properly @T.J.Crowder –  May 15 '18 at 16:47
  • Well, it's not being decode properly with it, either, according to your question. You only use `escape` if you're going to use `unescape` later. They're antiquated functions that have nothing to do with URI-encoding. To help you with what's wrong with the strings you're looping through, we need examples of those strings, and some information about how they were encoded in the first place. – T.J. Crowder May 15 '18 at 16:50
  • @T.J.Crowder they were encoded in php with "utf8_encode" and the problem is in the letter "ó" –  May 15 '18 at 16:52
  • We really need much more complete information than this. Okay, so they were encoded with `utf8_encode`. How did you then convey them to JavaScript? What is your actual goal (e.g., why encode them with `utf8_encode`)? – T.J. Crowder May 15 '18 at 16:57
  • 1
    @T.J.Crowder because in that way a can echo json data from php to js (after doing the "utf8_encode" ). The goal here is convert this encoded string "AntÃnio" to its original that is "António". The problem is in decoding the letter "Ã" to "ó" –  May 15 '18 at 17:04
  • 1
    Ah! You don't want to do that. :-) Instead, [use `json_encode`](https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript). For instance, if outputting to a JavaScript block: `var data = ;` (No parsing on the JS end; the JSON is valid JavaScript and the JavaScript parser parses it directly.) If responding to an ajax request, just `` and use `JSON.parse` on the JS side. Happy coding! – T.J. Crowder May 16 '18 at 06:41

0 Answers0