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

时间:2014-10-07 11:20:42

标签: php unicode utf-8 ascii non-latin

我正在开发一个需要翻译成40多种语言的项目。

我对大多数事情都进行了整理,但这一次真的让我感到烦恼。请看看这个,看看我需要什么:

<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>

获取一些俄语文本,一些泰语等,并将其弹出到我真棒的JS转换器中 - 它输出ASCII值 - 这些值我现在可以在PHP数组,html模板等中使用,一切都很好。

我正在为我的PHP翻译类编写一种新方法,将PHP语言数组转换为新语言 - 这就是问题所在。我怎样才能让PHP做这个JS做得很好的事情?我已经尝试过htmlentities,iconv等。我希望看到这样的翻译文本:

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

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

1 个答案:

答案 0 :(得分:0)

好吧,这似乎给了我以后的结果:

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

我现在可以看到我需要从浏览器复制并粘贴到我的新数组中的文本结果。