dongzj2015 2014-10-07 11:20
浏览 65

将非拉丁文本转换为ASCII条目 - 无需指定区域设置

I am working on a project that requires translation into 40+ languages.

I have most things all sorted but this one is really buggging me. Please take a look at this to see what I need:

<script type="text/javascript">
/* Purpose: Converts all non-latin based characters to their ASCII entity value
 * Usage: [String].encode()
 * Arguments: none
 * Returns: String
 */
String.prototype.encode = function() {
        return this.replace(/([^\x01-\x7E])/g, function(s) { return         "&#"+s.charCodeAt(0)+";"; });
};

/**
 * Converts all ASCII entity value characters into their unicode equivelant
 * @return (String)
 */
String.prototype.decode = function() {
        Number.prototype.toHex = function(pad) {
            var s = this.toString(16).toUpperCase();
            var v = "";
            if(typeof pad == "number") {
                    while(v.length + s.length < pad) {
                            v += "0";
                    }
            }
            return v + s;
    };
    return this.replace(/(&#([^;]*);)/g, function(s) { return unescape("%u"+        Number(RegExp.$2).toHex(4)); });
};

function translate() {
document.getElementById("txtOutput").value =         document.getElementById("txtInput").value.encode();
}
</script>

<div class="admin_block">

<h1>Translator</h1>
<p>Sometimes you need to add non latin Characters to the templates, php code and more. This tool will help you convert your text</p>
<h2>Type or paste non latin text in here</h2>
<form name="input" onmousemove="translate();">
    <textarea style="width: 900px" id="txtInput" onkeyup="translate();" onchange="translate();" onmouseup="translate();" onmousemove="translate();"></textarea>
</form>

<h2>Copy and paste this safe code below</h2>
<form onmousemove="translate();">
    <textarea style="width: 900px" id="txtOutput"></textarea><br />
    <button id="btnSelect" onclick="translate();document.getElementById('txtOutput').select(); return false;">Translate and select</button>
</form>

</div>

Take some Russian text, some thai etc and pop it into my awesome JS converter - it outputs the ASCII values - these values I can now use in PHP array, html templates etc and all is fine.

I am writing a new method for my PHP translation class to convert PHP language array into new languages - heres the issue. How can I get PHP to do what this JS is doing fine? I have tried htmlentities,iconv etc. I want to see translated text such as this:

&#1057;&#1086;&#1083;&#1086;&#1084;&#1086;&#1085;&#1086;&#1074;&#1099; &#1054;&#1089;&#1090;&#1088;&#1086;&#1074;&#1072;

Соломоновы Острова

  • 写回答

1条回答 默认 最新

  • douxian3170 2014-10-07 11:44
    关注

    well this seems to give me the result I am after:

    $s = htmlentities(mb_convert_encoding($s, 'HTML-ENTITIES', 'UTF-8'));
    

    I can now see the text result I need to copy from a browser and paste into my new array.

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大