php 将英文数字转换为阿拉伯数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3386835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert English numbers to Arabic numerals
提问by HyderA
How can i convert English numbers being retrieved from the database to Arabic numeral symbols in PHP.
如何将正在从数据库中检索到的英文数字转换为 PHP 中的阿拉伯数字符号。
EDIT: Here're some examples of what Arabic numerals look like. I live in an Arab country, so please don't go around explaining to me what Arabic numerals look like. If you can help, good and well. I don't need bogus ideas.
编辑:以下是一些阿拉伯数字外观的示例。我住在一个阿拉伯国家,所以请不要到处向我解释阿拉伯数字的样子。如果你能帮上忙,那很好。我不需要虚假的想法。
回答by Pekka
If you are referring to what Wikipedia calls eastern arabic / indic numerals, a simple replace operation should do.
如果您指的是维基百科所说的东方阿拉伯/印度数字,则应该执行简单的替换操作。
$western_arabic = array('0','1','2','3','4','5','6','7','8','9');
$eastern_arabic = array('?','?','?','?','?','?','?','?','?','?');
$str = str_replace($western_arabic, $eastern_arabic, $str);
回答by Nabil Kadimi
Definitions
定义
- Western arabic numerals: "1234567890".
- Eastern arabic numerals: "??????????".
- 西方阿拉伯数字:“1234567890”。
- 东方阿拉伯数字:“??????????”。
The answer
答案
I wrote a couple of functions (gist.github.com)a while back.
Demo (3v4l.org)
演示(3v4l.org)
echo arabic_w2e("1234567890"); // Outputs: ??????????
echo arabic_e2w("??????????"); // Outputs: 1234567890
Code
代码
<?php
/**
* Converts numbers in string from western to eastern Arabic numerals.
*
* @param string $str Arbitrary text
* @return string Text with western Arabic numerals converted into eastern Arabic numerals.
*/
function arabic_w2e($str)
{
$arabic_eastern = array('?', '?', '?', '?', '?', '?', '?', '?', '?', '?');
$arabic_western = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
return str_replace($arabic_western, $arabic_eastern, $str);
}
/**
* Converts numbers from eastern to western Arabic numerals.
*
* @param string $str Arbitrary text
* @return string Text with eastern Arabic numerals converted into western Arabic numerals.
*/
function arabic_e2w($str)
{
$arabic_eastern = array('?', '?', '?', '?', '?', '?', '?', '?', '?', '?');
$arabic_western = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
return str_replace($arabic_eastern, $arabic_western, $str);
}
回答by Mario Awad
Or you can just download and use this class from: http://www.phpclasses.org/package/6626-PHP-Convert-numbers-to-Arabic-representation.html(Tested and working). Cheers.
或者您可以从以下位置下载并使用此类:http: //www.phpclasses.org/package/6626-PHP-Convert-numbers-to-Arabic-representation.html(已测试和工作)。干杯。
回答by Wim ten Brink
Are you trying to convert e.g. Sixty-nine to ???? ?????? Try if you can call the Google Translate service or another translation service. Otherwise, you should write your own numeralparser and use your knowledge of the Arabic language to translate it to Arabic. (But using a translation service would be a lot easier.) (Then again, PHP might already have a function that writes out numbers and digits in the proper way.)
你想把例如六十九转换成 ???? ??????如果您可以致电 Google 翻译服务或其他翻译服务,请尝试。否则,您应该编写自己的数字解析器并使用您的阿拉伯语知识将其翻译成阿拉伯语。(但使用翻译服务会容易得多。)(再说一次,PHP 可能已经有一个以正确方式写出数字和数字的函数。)
回答by C_Rance
I would suggest u to try using resource file for different language especially if the contents are known. Example: 2 file language files, 1 named language_EN, another language_AR where langauge_EN is to store the values in english, and language_AR for arabic.
我建议你尝试使用不同语言的资源文件,特别是如果内容是已知的。示例:2 个文件语言文件,1 个命名为 language_EN,另一个 language_AR,其中 langauge_EN 用于存储英语值,而 language_AR 用于存储阿拉伯语。
so for instance u want store the number "1"
所以例如你想存储数字“1”
in langauge_EN, do something like this. number.one=1
在 langauge_EN 中,做这样的事情。number.one=1
then in langauge_AR, (pretend x is the number "1" in arabic, but I guess unicode is preferred) number.one=x
然后在 langauge_AR 中,(假设 x 是阿拉伯语中的数字“1”,但我想 unicode 是首选) number.one=x
so after u retrieve from database and knowing that it is number, your raw result comes out to be "one".
所以在你从数据库中检索并知道它是数字之后,你的原始结果就会变成“一”。
so use number.+[result from database] to load from the langauage_AR This method is quite widely used in webpages which allow toggling of language. So at any time u need to convert back to english, just change back to language_EN. This works not only for number. Gd Luck
所以使用 number.+[result from database] 从 langauage_AR 加载 这种方法在允许切换语言的网页中被广泛使用。所以在任何时候你需要转换回英语,只需改回language_EN。这不仅适用于数字。天运

