How to convert Indic Characters to Unicode Escaped characters

I am currently designing a mobile application for Android. The text and content are in the Local Indic Language, Tamil. For Welcome, the equivalent of Tamil translation is: வணக்கம் . Since Android cannot display Indic Text, I am converting it using a service called JavaScript String Escape.

So this works in this way:

  • Input: வணக்கம்
  • Output: u0BB5u0BA3u0B95u0BCDu0B95u0BAEu0BCD
  • How can I make this using JavaScript or PHP as I have huge loads of text to be converted and made it into JSON. Sample JSON:

    {
      "title": "u0BAEu0BB0u0BC1u0BA4u0BCDu0BA4u0BC1u0BB5u0BB0u0BBFu0BA9u0BCD u0BAAu0BC6u0BAFu0BB0u0BCD #1",
      "image": "http://www.exceptnothing.com/doctors/doc11.png",
      "rating": "u2713 u0B87u0BAAu0BCDu0BAAu0BC7u0BBEu0BA4u0BC1 u0BAAu0BBEu0BB0u0BCDu0B95u0BCDu0B95u0BB2u0BBEu0BAEu0BCD",
      "rating2": "",
      "releaseYear": "u0BA8u0BBEu0BB3u0BCD u0BAEu0BC1u0BB4u0BC1u0BB5u0BA4u0BC1u0BAEu0BCD u0BAAu0BBEu0BB0u0BCDu0B95u0BCDu0B95u0BB2u0BBEu0BAEu0BCD",
      "genre": ["u25B6 u0B87u0BA4u0BAF u0BA8u0BBFu0BAAu0BC1u0BA3u0BB0u0BCD"]
    }
    

    I also would like to know how to decode the above JSON and show it as வணக்கம் . Thanks in advance.


    What you are looking for is escape() in JavaScript and json_encode() in PHP. Open up your console and type the following:

    escape("வணக்கம்")
    

    And you will get the following back:

    "%u0BB5%u0BA3%u0B95%u0BCD%u0B95%u0BAE%u0BCD"
    

    So the first one is solved. To get back the original வணக்கம் from the above one, use unescape() :

    unescape("%u0BB5%u0BA3%u0B95%u0BCD%u0B95%u0BAE%u0BCD");
    

    Note: One thing to be noted is, both escape() and unescape() are deprecated. So you need to use encodeURIComponent and decodeURIComponent

    Preview

    Update for Server Side

    For encoding and decoding into JSON, it is better for you to use the PHP's built-in function. The same escape() can also be used in PHP as json_encode() , they both give the same result.

    json_encode("வணக்கம்");
    => "%u0BB5%u0BA3%u0B95%u0BCD%u0B95%u0BAE%u0BCD"
    

    Also, see JavaScript: Escaping Special Characters for more information. Hope this helps. :)

    链接地址: http://www.djcxy.com/p/89880.html

    上一篇: 没有favicon的请求

    下一篇: 如何将印度语字符转换为Unicode转义字符