Today Ayon came up with a problem that he needs to convert English digits to Bangla. The Input would be something like “1 ডলার = 81.55 টাকা” and the output should be “১ ডলার = ৮১.৫৫ টাকা”. How to do it?
Its very easy to do. In fact most developers will be able to do it within few minutes. I just want to share my solution.
I use PHP’s str_replace function.
[code language=”PHP”] $bn_digits=array(‘০’,’১’,’২’,’৩’,’৪’,’৫’,’৬’,’৭’,’৮’,’৯’);
$output = str_replace(range(0, 9),$bn_digits, $input); [/code]
Thats it. You can wrap it with a function and re use it.
Your solution helps me. Thanks.
thanks! this is helpful!
Thanks….. 🙂
A lot of thanks..its really awesome..
verty nice
Is it possible in c#?
Obviously its possible.
bangla to english and english to bangla with same method in c#:
private string SwitchEngBan(string number)
{
string en = “1234567890.,”;
string bn = “১২৩৪৫৬৭৮৯০.,”;
return number.Select(o => en.Contains(o)
? bn.Substring(en.IndexOf(o), 1)
: en.Substring(bn.IndexOf(o), 1))
.Aggregate((a, b) => a + b);
}
Dear,
Your post is very useful. But I am a noob in developing PHP.
If I want to use it in WordPress theme, where I need to put the code?
Can you suggest me? I would love that. Very thankful to you.
Probably in `functions.php`. But I am not expert in wordpress. Check their guide.
What is the solution in javascript? 🙁
Its much simpler in JS too.
var bn_digits = ["০", "১", "২", "৩", "৪", ....]
function tr_digits(s){
return s.replace(/\d/, function(a){
return bn_digits[a]
})
}
Then you just call tr_digits(“312 taka”).
how it execute mysql query
I don’t get you. If you are asking about how to do it my MySQL, my answer is “Replace the number with this function then pass it to mysql query”