0

I am try to load city name from the XML file using javascript AJAX and finally success on them.

var region=Ahmadābād,Sūrat,Vadodara,Rājkot,Bhāvnagar,Jāmnagar,Nadiād,Gāndhīnagar,Jūnāgadh,Surendranagar

This is my output; in this output some charcter are non standard US ASCII and I want to change into normal chars, like:

var region:- Ahmadabad,Surat,Vadodara,Rajkot,Bhavnagar,Jamnagar,Nadiad,Gandhinagar,Junagadh,Surendranagar

How can I do that?

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
Jack Php
  • 577
  • 2
  • 7
  • 25
  • possible duplicate of [Convert non-ASCII characters (umlauts, accents...) to their closest ASCII equivalent (slug creation)](http://stackoverflow.com/questions/11815883/convert-non-ascii-characters-umlauts-accents-to-their-closest-ascii-equiva) – Adriano Repetti May 08 '13 at 10:57
  • 1
    Why do you want to break the spelling of names? It would be more constructive to ask why you think you need to force names to use a character repertoire defined in the 1960s. – Jukka K. Korpela May 08 '13 at 10:58

1 Answers1

0

This is a pure javascript solution though it is not optimal and might perform not well:

// create a character map to convert one char to another
var charMap = {
    "ā" : "a",
    "ū" : "u"
};
var region="Ahmadābād,Sūrat,Vadodara,Rājkot,Bhāvnagar,Jāmnagar,Nadiād,Gāndhīnagar,Jūnāgadh,Surendranagar";

// split original string into char array
var chars = region.split('');
// init new array for conversion result
var charsConverted = [];

// convert characters one by one
for(var i = 0; i < chars.length; i++){
    var char = chars[i];
    // this will try to use a matching char from char map
    // will use original if no pair found in charMap
    charsConverted.push( charMap[char] || char);
}

// join array to string
var result = charsConverted.join('');

alert(region);
alert(result);

Again this is just an idea and might need a lot of tweaking.

Code in action: http://jsfiddle.net/L5Yzf/

HTH

Ramunas
  • 3,853
  • 1
  • 30
  • 31