仅将英语数字转换为指定div的波斯语/阿拉伯语 [英] Convert English numbers to Persian/Arabic only for a specified div
问题描述
我知道这个问题在这里已经回答了很多次,但我还没有一个确切的答案..我需要通过一些javascript将英文字母转换为波斯语/阿拉伯字母,但不是整个页面,但只有为一个div或更多。
我遇到了这些代码,但不知道哪一个是最好的。
function convert($ string){
$ persian = array('0','1','2','3','4 ','5','6','7','8','9');
$ num = range(0,9);
return str_replace($ persian,$ num,$ string);
}
我需要该源才能在一个div类上实现。 p>
例如:
< div class =demo> 12345< / div>
应更改为
< div class =demo> 12345< / div>
我不相信任何代码示例提供的是JavaScript,第一个是语法上的关闭,但是缺少 range()
方法和 new
c $ c> array()定义。第二个是Java。
为了实现你所需要的,你可以将每个要翻译的HTML元素的文本转换为数组,并逐步执行,字符通过Regex来查看是否找到一个数字。如果是,您可以在将数组加回到一起之前进行简单的替换。像这样:
var arabicNumbers = ['0','1','2','3','4 ','5','6','7','8','9'];
$('。translate')。text(function(i,v){
var chars = v.split('');
for(var i = 0; i& chars.length; i ++){
if(/\d/.test(chars[i])){
chars [i] = arabicNumbers [chars [i]];
}
}
return chars.join('');
})
I know this question has been replied many times here, but I still haven't got an exact answer.. I need to convert English letters to Persian/Arabic letters by some javascript, but not for the entire page, but only for a div or more. like only for a specific class.
I have come across these codes, but don't know which one are the best to use.
function convert($string) {
$persian = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');
$num = range(0, 9);
return str_replace($persian, $num, $string);
}
I need exact that source to implement only on one div-class.
For example:
<div class="demo">12345</div>
should change to
<div class="demo">۱۲۳۴۵</div>
I don't believe either of the code samples you provided are JavaScript, the first is close syntactically, but is missing the range()
method and new
on the array()
definition. The second is Java.
To achieve what you require you could convert the text of each of the HTML elements you want to translate to an array and step through them, checking each character via Regex to see if a number was found. If it was, you can do a simple replacement before joining the array back together. Something like this:
var arabicNumbers = ['۰', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
$('.translate').text(function(i, v) {
var chars = v.split('');
for (var i = 0; i < chars.length; i++) {
if (/\d/.test(chars[i])) {
chars[i] = arabicNumbers[chars[i]];
}
}
return chars.join('');
})
这篇关于仅将英语数字转换为指定div的波斯语/阿拉伯语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!