Home PHP The Easiest Method for Converting Digits Between Languages

The Easiest Method for Converting Digits Between Languages

0
17
Multilingual Digit Conversion
Multilingual Digit Conversion

In this article, I’ll demonstrate how to convert digits between languages, specifically from English to Bengali and vice versa.

First, let’s declare the variables that will store the digits for each language. For example:

$bn = ['১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯', '০'];

$en = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];

Create a function that will convert the Bengali to English

public static function bn2en(string $number)
{
    return str_replace(self::$bn, self::$en, $number);
}

Another function will transform the English to Bengali digits.

public static function en2bn(string $number)
{
    return str_replace(self::$en, self::$bn, $number);
}

That’s it. Here is a complete example

class Converter
{
    public static array $bn = ['১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯', '০'];

    public static array $en = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];

    public static function bn2en(string $number): array|string
    {
        return str_replace(self::$bn, self::$en, $number);
    }

    public static function en2bn(string $number): array|string
    {
        return str_replace(self::$en, self::$bn, $number);
    }
}

 

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here