如何使用javascript或php将Unicode转换为Preeti(ASCII)?

时间:2018-10-24 05:43:30

标签: javascript php unicode

我是javascript的新手,最近加入了学士学位。在我的第一学期中,我获得了一个将Unicode转换为ASCII代码的Preeti(尼泊尔语字体)的项目。转换字典列为:

unicodeToPreetiDict = \

{
    "अ": "c",
    "आ": "cf",
    "ा": "f",
    "इ": "O",
    "ई": "O{",
    "र्": "{",
    "उ": "p",
    "ए": "P",
    "े": "]",
    "ै": "}",
    "ो": "f]",
    "ौ": "f}",
    "ओ": "cf]",
    "औ": "cf}",
    "ं": "+",
    "ँ": "F",
    "ि": "l",
    "ी": "L",
    "ु": "'",
    "ू": '"',
    "क": "s",
    "ख": "v",
    "ग": "u",
    "घ": "3",
    "ङ": "ª",
    "च": "r",
    "छ": "5",
    "ज": "h",
    "झ": "´",
    "ञ": "`",
    "ट": "6",
    "ठ": "7",
    "ड": "8",
    "ढ": "9",
    "ण": "0f",
    "त": "t",
    "थ": "y",
    "द": "b",
    "ध": "w",
    "न": "g",
    "प": "k",
    "फ": "km",
    "ब": "a",
    "भ": "e",
    "म": "d",
    "य": "o",
    "र": "/",
    "रू": "?",
    "ृ": "[",
    "ल": "n",
    "व": "j",
    "स": ";",
    "श": "z",
    "ष": "if",
    "ज्ञ": "1",
    "ह": "x",
    "१": "!",
    "२": "@",
    "३": "#",
    "४": "$",
    "५": "%",
    "६": "^",
    "७": "&",
    "८": "*",
    "९": "(",
    "०": ")",
    "।": ".",
    "्": "\\",
    "ऊ": "pm",
    "-": " ",
    "(": "-",
    ")": "_"
}

字符的左部分是Unicode,而右部分则是Preeti字体。

您可以在此示例站点中编写代码后检查转换: http://unicode.shresthasushil.com.np/

如果我们以लम(unicode)的形式提供输入,则必须将其转换为Preeti。我需要javascript或php中的源代码才能将unicode转换为preeti。

2 个答案:

答案 0 :(得分:1)

一种方法是使用开关对其进行硬编码

function convert(x)
{
  switch(x)
    {
        case "अ":
            return "c";
        case "आ":
            return "cf";
        case "ा":
            return "f";
        case "इ":
            return "O";
        case "ई":
            return "O{";
        case "र्":
            return "{";
        case "उ":
            return "p";
        case "ए":
            return "P";
        case "े":
            return "]";
        case "ै":
            return "}";
        case "ो":
            return "f]";
        case "ौ":
            return "f}";
        case "ओ":
            return "cf]";
        case "औ":
            return "cf}";
        case "ं":
            return "+";
        case "ँ":
            return "F";
        case "ि":
            return "l";
        case "ी":
            return "L";
        case "ु":
            return "'";
        case "ू":
            return '"';
        case "क":
            return "s";
        case "ख":
            return "v";
        case "ग":
            return "u";
        case "घ":
            return "3";
        case "ङ":
            return "ª";
        case "च":
            return "r";
        case "छ":
            return "5";
        case "ज":
            return "h";
        case "झ":
            return "´";
        case "ञ":
            return "`";
        case "ट":
            return "6";
        case "ठ":
            return "7";
        case "ड":
            return "8";
        case "ढ":
            return "9";
        case "ण":
            return "0f";
        case "त":
            return "t";
        case "थ":
            return "y";
        case "द":
            return "b";
        case "ध":
            return "w";
        case "न":
            return "g";
        case "प":
            return "k";
        case "फ":
            return "km";
        case "ब":
            return "a";
        case "भ":
            return "e";
        case "म":
            return "d";
        case "य":
            return "o";
        case "र":
            return "/";
        case "रू":
            return "?";
        case "ृ":
            return "[";
        case "ल":
            return "n";
        case "व":
            return "j";
        case "स":
            return ";";
        case "श":
            return "z";
        case "ष":
            return "if";
        case "ज्ञ":
            return "1";
        case "ह":
            return "x";
        case "१":
            return "!";
        case "२":
            return "@";
        case "३":
            return "#";
        case "४":
            return "$";
        case "५":
            return "%";
        case "६":
            return "^";
        case "७":
            return "&";
        case "८":
            return "*";
        case "९":
            return "(";
        case "०":
            return ")";
        case "।":
            return ".";
        case "्":
            return "\\";
        case "ऊ":
            return "pm";
        case "-":
            return " ";
        case "(":
            return "-";
        case ")":
            return "_";
    }  
}
console.log(convert("ऊ"));

当您确定它可以按预期工作并完成测试时,可以将其粉碎成一行,以免浪费文本编辑器空间。

function convert(x){
  switch(x) {case "अ": return "c"; case "आ": return "cf"; case "ा": return "f"; case "इ": return "O"; case "ई": return "O{"; case "र्": return "{"; case "उ": return "p"; case "ए": return "P"; case "े": return "]"; case "ै": return "}"; case "ो": return "f]"; case "ौ": return "f}"; case "ओ": return "cf]"; case "औ": return "cf}"; case "ं": return "+"; case "ँ": return "F"; case "ि": return "l"; case "ी": return "L"; case "ु": return "'"; case "ू": return '"'; case "क": return "s"; case "ख": return "v"; case "ग": return "u"; case "घ": return "3"; case "ङ": return "ª"; case "च": return "r"; case "छ": return "5"; case "ज": return "h"; case "झ": return "´"; case "ञ": return "`"; case "ट": return "6"; case "ठ": return "7"; case "ड": return "8"; case "ढ": return "9"; case "ण": return "0f"; case "त": return "t"; case "थ": return "y"; case "द": return "b"; case "ध": return "w"; case "न": return "g"; case "प": return "k"; case "फ": return "km"; case "ब": return "a"; case "भ": return "e"; case "म": return "d"; case "य": return "o"; case "र": return "/"; case "रू": return "?"; case "ृ": return "["; case "ल": return "n"; case "व": return "j"; case "स": return ";"; case "श": return "z"; case "ष": return "if"; case "ज्ञ": return "1"; case "ह": return "x"; case "१": return "!"; case "२": return "@"; case "३": return "#"; case "४": return "$"; case "५": return "%"; case "६": return "^"; case "७": return "&"; case "८": return "*"; case "९": return "("; case "०": return ")"; case "।": return "."; case "्": return "\\"; case "ऊ": return "pm"; case "-": return " "; case "(": return "-"; case ")": return "_"; } 
} 

因此,如果您有一串unicode字符,则您的代码可能如下所示:

function convert(x){
  switch(x) {case "अ": return "c"; case "आ": return "cf"; case "ा": return "f"; case "इ": return "O"; case "ई": return "O{"; case "र्": return "{"; case "उ": return "p"; case "ए": return "P"; case "े": return "]"; case "ै": return "}"; case "ो": return "f]"; case "ौ": return "f}"; case "ओ": return "cf]"; case "औ": return "cf}"; case "ं": return "+"; case "ँ": return "F"; case "ि": return "l"; case "ी": return "L"; case "ु": return "'"; case "ू": return '"'; case "क": return "s"; case "ख": return "v"; case "ग": return "u"; case "घ": return "3"; case "ङ": return "ª"; case "च": return "r"; case "छ": return "5"; case "ज": return "h"; case "झ": return "´"; case "ञ": return "`"; case "ट": return "6"; case "ठ": return "7"; case "ड": return "8"; case "ढ": return "9"; case "ण": return "0f"; case "त": return "t"; case "थ": return "y"; case "द": return "b"; case "ध": return "w"; case "न": return "g"; case "प": return "k"; case "फ": return "km"; case "ब": return "a"; case "भ": return "e"; case "म": return "d"; case "य": return "o"; case "र": return "/"; case "रू": return "?"; case "ृ": return "["; case "ल": return "n"; case "व": return "j"; case "स": return ";"; case "श": return "z"; case "ष": return "if"; case "ज्ञ": return "1"; case "ह": return "x"; case "१": return "!"; case "२": return "@"; case "३": return "#"; case "४": return "$"; case "५": return "%"; case "६": return "^"; case "७": return "&"; case "८": return "*"; case "९": return "("; case "०": return ")"; case "।": return "."; case "्": return "\\"; case "ऊ": return "pm"; case "-": return " "; case "(": return "-"; case ")": return "_"; } 
}


var str = "आअाअअ";


for(var i = 0; i < str.length; i++)
{
    console.log(convert(str[i]));
}

确保在html文件中有一个<meta charset="utf-8">标记。


如果由于某种原因您无法使用此方法,那么我发现还有其他一些关于使用charCodeAt进行拆分的方法。

这种方法的示例如下:

var str = "आअाअअ";


var arr = stringToArray(str);
console.log(arr);



function convert(x){
  switch(x) {case "अ": return "c"; case "आ": return "cf"; case "ा": return "f"; case "इ": return "O"; case "ई": return "O{"; case "र्": return "{"; case "उ": return "p"; case "ए": return "P"; case "े": return "]"; case "ै": return "}"; case "ो": return "f]"; case "ौ": return "f}"; case "ओ": return "cf]"; case "औ": return "cf}"; case "ं": return "+"; case "ँ": return "F"; case "ि": return "l"; case "ी": return "L"; case "ु": return "'"; case "ू": return '"'; case "क": return "s"; case "ख": return "v"; case "ग": return "u"; case "घ": return "3"; case "ङ": return "ª"; case "च": return "r"; case "छ": return "5"; case "ज": return "h"; case "झ": return "´"; case "ञ": return "`"; case "ट": return "6"; case "ठ": return "7"; case "ड": return "8"; case "ढ": return "9"; case "ण": return "0f"; case "त": return "t"; case "थ": return "y"; case "द": return "b"; case "ध": return "w"; case "न": return "g"; case "प": return "k"; case "फ": return "km"; case "ब": return "a"; case "भ": return "e"; case "म": return "d"; case "य": return "o"; case "र": return "/"; case "रू": return "?"; case "ृ": return "["; case "ल": return "n"; case "व": return "j"; case "स": return ";"; case "श": return "z"; case "ष": return "if"; case "ज्ञ": return "1"; case "ह": return "x"; case "१": return "!"; case "२": return "@"; case "३": return "#"; case "४": return "$"; case "५": return "%"; case "६": return "^"; case "७": return "&"; case "८": return "*"; case "९": return "("; case "०": return ")"; case "।": return "."; case "्": return "\\"; case "ऊ": return "pm"; case "-": return " "; case "(": return "-"; case ")": return "_"; } 
}


function stringToArray() {
  var i = 0,
    arr = [],
    codePoint;
  while (!isNaN(codePoint = knownCharCodeAt(str, i))) {
    arr.push(String.fromCodePoint(codePoint));
    i++;
  }
  return arr;
}

if (!String.fromCodePoint) {
// ES6 Unicode Shims 0.1 , © 2012 Steven Levithan , MIT License
    String.fromCodePoint = function fromCodePoint () {
        var chars = [], point, offset, units, i;
        for (i = 0; i < arguments.length; ++i) {
            point = arguments[i];
            offset = point - 0x10000;
            units = point > 0xFFFF ? [0xD800 + (offset >> 10), 0xDC00 + (offset & 0x3FF)] : [point];
            chars.push(String.fromCharCode.apply(null, units));
        }
        return chars.join("");
    }
}


function knownCharCodeAt(str, idx) {
  str += '';
  var code,
      end = str.length;

  var surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
  while ((surrogatePairs.exec(str)) != null) {
    var li = surrogatePairs.lastIndex;
    if (li - 2 < idx) {
      idx++;
    }
    else {
      break;
    }
  }

  if (idx >= end || idx < 0) {
    return NaN;
  }

  code = str.charCodeAt(idx);

  var hi, low;
  if (0xD800 <= code && code <= 0xDBFF) {
    hi = code;
    low = str.charCodeAt(idx + 1);
    // Go one further, since one of the "characters"
    // is part of a surrogate pair
    return ((hi - 0xD800) * 0x400) +
      (low - 0xDC00) + 0x10000;
  }
  return code;
}

我强烈建议您使用我为您制作的第一种方法,因为它易于控制。

答案 1 :(得分:0)

您使用数组在PHP的str_replace中进行搜索/替换

$translated_msg = str_replace(["अ","आ","ा","इ","ई","र्","उ","ए","े","ै","ो","ौ","ओ","औ","ं","ँ","ि","ी","ु","ू","क","ख","ग","घ","ङ","च","छ","ज","झ","ञ","ट","ठ","ड","ढ","ण","त","थ","द","ध","न","प","फ","ब","भ","म","य","र","रू","ृ","ल","व","स","श","ष","ज्ञ","ह","१","२","३","४","५","६","७","८","९","०","।","्","ऊ","-","(",")"], ["3","ª","r","5","h","´","`","6","7","8","9","0f","t","y","b","w","g","k","km","a","e","d","o","/","?","[","n","j",";","z","if","1","x","!","@","#","$","%","^","&","*","(",")",".","\\","pm"," ","-","_"], $some_msg);