php 将波斯/阿拉伯数字转换为英文数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11766726/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:01:16  来源:igfitidea点击:

convert Persian/Arabic numbers to English numbers

phpunicode

提问by Root

How can I convert Persian/Arabic numbersto English numberswith a simple function ?

如何使用简单的函数将波斯/阿拉伯数字转换为英文数字

Persian/Arabic numbers:

波斯/阿拉伯数字:

?   //  -> 0
?   //  -> 1
?   //  -> 2
?   //  -> 3
?   //  -> 4
?   //  -> 5
?   //  -> 6
?   //  -> 7
?   //  -> 8
?   //  -> 9

numbers over the unicode :

Unicode 上的数字:

$num0="۰";
$num1="۱";
$num2="۲";
$num3="۳";
$num4="۴";
$num5="۵";
$num6="۶";
$num7="۷";
$num8="۸";
$num9="۹";

采纳答案by Root

Because of people at Persian & Arabic region maybe use each other language , this is the complete solution to convert both type .

由于波斯语和阿拉伯语地区的人们可能会使用其他语言,这是转换两种类型的完整解决方案。

function convert2english($string) {
    $newNumbers = range(0, 9);
    // 1. Persian HTML decimal
    $persianDecimal = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');
    // 2. Arabic HTML decimal
    $arabicDecimal = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩');
    // 3. Arabic Numeric
    $arabic = array('?', '?', '?', '?', '?', '?', '?', '?', '?', '?');
    // 4. Persian Numeric
    $persian = array('?', '?', '?', '?', '?', '?', '?', '?', '?', '?');

    $string =  str_replace($persianDecimal, $newNumbers, $string);
    $string =  str_replace($arabicDecimal, $newNumbers, $string);
    $string =  str_replace($arabic, $newNumbers, $string);
    return str_replace($persian, $newNumbers, $string);
}

回答by Palladium

Here's a short function:

这是一个简短的函数:

function convert($string) {
    $persian = ['?', '?', '?', '?', '?', '?', '?', '?', '?', '?'];
    $arabic = ['?', '?', '?', '?', '?', '?', '?', '?', '?','?'];

    $num = range(0, 9);
    $convertedPersianNums = str_replace($persian, $num, $string);
    $englishNumbersOnly = str_replace($arabic, $num, $convertedPersianNums);

    return $englishNumbersOnly;
}

You can use the unicode instead of the characters in $persian(I think).

您可以使用 unicode 而不是$persian(我认为)中的字符。

回答by Saeid Tahmuresi

I use this function. It's convert both Persian and Arabic numbers to English:

我用这个功能。它将波斯和阿拉伯数字转换为英语:

function faTOen($string) {
    return strtr($string, array('?'=>'0', '?'=>'1', '?'=>'2', '?'=>'3', '?'=>'4', '?'=>'5', '?'=>'6', '?'=>'7', '?'=>'8', '?'=>'9', '?'=>'0', '?'=>'1', '?'=>'2', '?'=>'3', '?'=>'4', '?'=>'5', '?'=>'6', '?'=>'7', '?'=>'8', '?'=>'9'));
}

sample:

样本:

echo faTOen("????????????????????"); // 01234567890123456789

Also you can use same way to convert English to Persian:

您也可以使用相同的方式将英语转换为波斯语:

function enToFa($string) {
    return strtr($string, array('0'=>'?','1'=>'?','2'=>'?','3'=>'?','4'=>'?','5'=>'?','6'=>'?','7'=>'?','8'=>'?','9'=>'?'));
}

Or English to Arabic:

或英语到阿拉伯语:

function enToAr($string) {
    return strtr($string, array('0'=>'?','1'=>'?','2'=>'?','3'=>'?','4'=>'?','5'=>'?','6'=>'?','7'=>'?','8'=>'?','9'=>'?'));
}

回答by iman

this is better. we have two type of digits in arabic and persian. we must change all.

这个更好。我们有阿拉伯语和波斯语两种类型的数字。我们必须改变一切。

function convert($string) {
    $persinaDigits1= array('?', '?', '?', '?', '?', '?', '?', '?', '?', '?');
    $persinaDigits2= array('?', '?', '?', '?', '?', '?', '?', '?', '?', '?');
    $allPersianDigits=array_merge($persinaDigits1, $persinaDigits2);
    $replaces = array('0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9');
    return str_replace($allPersianDigits, $replaces , $string);
}


thanks @Palladium


谢谢@Palladium

Be Careful when copying the code! Check twice how the array is presented in your editor or your will be in trouble!

复制代码时要小心!检查两次数组在编辑器中的显示方式,否则您会遇到麻烦!

回答by Ignacio Vazquez-Abrams

$fmt = numfmt_create('fa', NumberFormatter::DECIMAL);
echo numfmt_parse($fmt, "?") . "\n";
// 5

回答by Morteza Sepehri Niya

For convert all persian number to english format you can use this function:

要将所有波斯数字转换为英文格式,您可以使用此功能:

function Convertnumber2english($srting) {

        $srting = str_replace('?', '0', $srting);
        $srting = str_replace('?', '1', $srting);
        $srting = str_replace('?', '2', $srting);
        $srting = str_replace('?', '3', $srting);
        $srting = str_replace('?', '4', $srting);
        $srting = str_replace('?', '5', $srting);
        $srting = str_replace('?', '6', $srting);
        $srting = str_replace('?', '7', $srting);
        $srting = str_replace('?', '8', $srting);
        $srting = str_replace('?', '9', $srting);

        return $srting;
    }

回答by Aditya P Bhatt

Very easy way to convert Arabic numbers to English numberdigits using below function:

使用以下函数将阿拉伯数字转换为英文数字的非常简单的方法:

function ArtoEnNumeric($string) {
    return strtr($string, array('?'=>'0', '?'=>'1', '?'=>'2', '?'=>'3', '?'=>'4', '?'=>'5', '?'=>'6', '?'=>'7', '?'=>'8', '?'=>'9', '?'=>'0', '?'=>'1', '?'=>'2', '?'=>'3', '?'=>'4', '?'=>'5', '?'=>'6', '?'=>'7', '?'=>'8', '?'=>'9'));
}

echo ArtoEnNumeric('????????'); 
Output = 60631638

回答by 1N8A

$sting= '???????????'; 

// search stings
$seachstrings = array("?", "?", "?", "?", "?", "?", "?", "?", "?", "?"); 

// replace strings 
$replacestrings= array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0"); 

// replace function 
$result= str_replace($seachstrings , $replacestrings, $sting); 

print_r($result); 

回答by Ali Motameni

Two useful functions:

两个有用的功能:

First convert to English number:

先转换成英文数字:

function convert_to_en_number($string) {
    $persian = ['?', '?', '?', '?', '?', '?', '?', '?', '?', '?'];
    $arabic = ['?', '?', '?', '?', '?', '?', '?', '?', '?','?'];

    $num = range(0, 9);
    $convertedPersianNums = str_replace($persian, $num, $string);
    $englishNumbersOnly = str_replace($arabic, $num, $convertedPersianNums);

    return $englishNumbersOnly;
}

Second convert to Persian number:

第二次转换为波斯数字:

function convert_to_fa_number($string) {
    $num = range(0, 9);
    $arabic = ['?', '?', '?', '?', '?', '?', '?', '?', '?','?'];
    $persian = ['?', '?', '?', '?', '?', '?', '?', '?', '?', '?'];

    $convertedEnglishNums = str_replace($num, $persian, $string);
    $persianNumbersOnly = str_replace($arabic, $persian, $convertedEnglishNums);

    return $persianNumbersOnly;
}

回答by iSWORD

I know this is a six-year old question, but I just came across this and developed a generic solution and I'd like to share it here for future readers.

我知道这是一个六年前的问题,但我刚刚遇到了这个问题并开发了一个通用的解决方案,我想在这里分享给未来的读者。

Basically I use the NumberFormatterto generate the numbers in any foreign language, then I replace the numbers in the input string with the ones from English.

基本上我使用NumberFormatter来生成任何外语的数字,然后我用英语中的数字替换输入字符串中的数字。

It should work for any language, but I wrote mine for Arabic.

它应该适用于任何语言,但我为阿拉伯语写了我的。

/**
 * Replace Arabic numbers by English numbers in a string
 *
 * @param $value string A string containing Arabic numbers.
 * @param $source_language string The locale to convert chars from.
 *
 * @return string The string with Arabic numbers replaced by English numbers
 */
function englishifyNumbers ($value, $source_language = 'ar') {
    // Remove any direction overriding chars [LRO (U+202D)]
    $value = trim($value);

    // Create an Arabic number formatter to generate Arabic numbers
    $fmt = new \NumberFormatter(
        $source_language, 
        \NumberFormatter::PATTERN_DECIMAL
    );

    // Create an array of English numbers to use for replacement
    $english_numbers = range(0, 9);

    // Convert the English numbers to Arabic numbers using the formatter
    $arabic_numbers = array_map(function ($n) use ($fmt) {
        // Trim to remove direction overriding chars [PDF (U+202C)]
        return trim($fmt->format($n));

    }, $english_numbers);

    // Replace the numbers and return the result
    return str_replace($arabic_numbers, $english_numbers, $value);
}