将英文数字转换为阿拉伯数字

发布于 2024-09-12 14:40:38 字数 388 浏览 6 评论 0原文

如何将从数据库检索到的英文数字转换为 PHP 中的阿拉伯数字符号。

编辑:以下是阿拉伯数字的一些示例。我住在阿拉伯国家,所以请不要到处向我解释阿拉伯数字是什么样子。如果你能帮忙,那就太好了。我不需要虚假的想法。

http:// /upload.wikimedia.org/wikipedia/commons/thumb/2/21/Arabic_numerals-en.svg/500px-Arabic_numerals-en.svg.png

How can i convert English numbers being retrieved from the database to Arabic numeral symbols in 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.

http://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Arabic_numerals-en.svg/500px-Arabic_numerals-en.svg.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

葮薆情 2024-09-19 14:40:38

如果您指的是维基百科所说的东方阿拉伯/印度数字,则应该执行简单的替换操作。

$western_arabic = array('0','1','2','3','4','5','6','7','8','9');
$eastern_arabic = array('٠','١','٢','٣','٤','٥','٦','٧','٨','٩');

$str = str_replace($western_arabic, $eastern_arabic, $str);

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);
撑一把青伞 2024-09-19 14:40:38

定义

  • 西方阿拉伯数字:“1234567890”。
  • 东方阿拉伯数字:“١٢٣٤٥٦٧٨٩٠”。

我写的答案

几个函数(gist.github.com) 不久前。

演示(3v4l.org)

    echo arabic_w2e("1234567890"); // Outputs: ١٢٣٤٥٦٧٨٩٠
    echo arabic_e2w("١٢٣٤٥٦٧٨٩٠"); // Outputs: 1234567890

代码

<?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);
}

Definitions

  • Western arabic numerals: "1234567890".
  • Eastern arabic numerals: "١٢٣٤٥٦٧٨٩٠".

The answer

I wrote a couple of functions (gist.github.com) a while back.

Demo (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);
}
梦归所梦 2024-09-19 14:40:38

或者您可以从以下位置下载并使用此类: http://www.phpclasses.org/package/6626-PHP-Convert-numbers-to-Arabic-representation.html(已测试且有效)。干杯。

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.

蓝梦月影 2024-09-19 14:40:38

我建议您尝试使用不同语言的资源文件,特别是在内容已知的情况下。
例子:
2 个语言文件,其中 1 个名为 language_EN,另一个名为 language_AR
其中 langauge_EN 是用英语存储值,而 language_AR 是用阿拉伯语存储值。

例如,您想将数字“1”存储

在 langauge_EN 中,请执行以下操作。
number.one=1

然后在 langauge_AR 中,(假设 x 是阿拉伯语中的数字“1”,但我猜 unicode 是首选)
number.one=x

所以在你从数据库检索并知道它是数字之后,
你的原始结果是“一”。

所以使用 number.+[result from database] 从 langauage_AR 加载
这种方法在允许切换语言的网页中使用相当广泛。
因此,任何时候您需要转换回英语,只需改回 language_EN 即可。
这不仅适用于数字。
幸运之神

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.

so for instance u want store the number "1"

in langauge_EN, do something like this.
number.one=1

then in langauge_AR, (pretend x is the number "1" in arabic, but I guess unicode is preferred)
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

紧拥背影 2024-09-19 14:40:38

阿拉伯数字:

function to_arabic_number($number)
{
    $number = str_replace("1","۱",$number);
    $number = str_replace("2","۲",$number);
    $number = str_replace("3","۳",$number);
    $number = str_replace("4","٤",$number);
    $number = str_replace("5","٥",$number);
    $number = str_replace("6","٦",$number);
    $number = str_replace("7","۷",$number);
    $number = str_replace("8","۸",$number);
    $number = str_replace("9","۹",$number);
    $number = str_replace("0","۰",$number);
    return $number;
}

波斯数字:

function to_persian_number($number)
{
    $number = str_replace("1","۱",$number);
    $number = str_replace("2","۲",$number);
    $number = str_replace("3","۳",$number);
    $number = str_replace("4","۴",$number);
    $number = str_replace("5","۵",$number);
    $number = str_replace("6","۶",$number);
    $number = str_replace("7","۷",$number);
    $number = str_replace("8","۸",$number);
    $number = str_replace("9","۹",$number);
    $number = str_replace("0","۰",$number);
    return $number;
}

Arabic Numbers:

function to_arabic_number($number)
{
    $number = str_replace("1","۱",$number);
    $number = str_replace("2","۲",$number);
    $number = str_replace("3","۳",$number);
    $number = str_replace("4","٤",$number);
    $number = str_replace("5","٥",$number);
    $number = str_replace("6","٦",$number);
    $number = str_replace("7","۷",$number);
    $number = str_replace("8","۸",$number);
    $number = str_replace("9","۹",$number);
    $number = str_replace("0","۰",$number);
    return $number;
}

Persian Numbers:

function to_persian_number($number)
{
    $number = str_replace("1","۱",$number);
    $number = str_replace("2","۲",$number);
    $number = str_replace("3","۳",$number);
    $number = str_replace("4","۴",$number);
    $number = str_replace("5","۵",$number);
    $number = str_replace("6","۶",$number);
    $number = str_replace("7","۷",$number);
    $number = str_replace("8","۸",$number);
    $number = str_replace("9","۹",$number);
    $number = str_replace("0","۰",$number);
    return $number;
}
以可爱出名 2024-09-19 14:40:38

您是否正在尝试将例如 69 转换为 กกรรรรรร ?尝试是否可以调用 Google 翻译服务或其他翻译服务。否则,您应该编写自己的数字解析器并使用您的阿拉伯语言知识来翻译它到阿拉伯语。 (但是使用翻译服务会容易得多。)(话又说回来,PHP 可能已经有一个以正确方式写出数字和数字的函数。)

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 numeral parser 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.)

新一帅帅 2024-09-19 14:40:38

使用 strtr 的简单解决方案:

strtr($str, ['٠','١','٢','٣','٤','٥','٦','٧','٨','٩']);

从东阿拉伯语转换为阿拉伯语:

strtr($str, array_flip(['٠','١','٢','٣','٤','٥','٦','٧','٨','٩']));

Simple solution with strtr:

strtr($str, ['٠','١','٢','٣','٤','٥','٦','٧','٨','٩']);

To convert from Eastern Arabic to Arabic:

strtr($str, array_flip(['٠','١','٢','٣','٤','٥','٦','٧','٨','٩']));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文