how to change to english number to nepali number in php

0 votes

I want to change the English numerals to Nepali one up to 2 digits. For example: if I enter 1 it should return १ and if I enter 41 it should return ४१ and I have to store ४१ in DB and show it in the front end. 
This is what I have done till now:

function convertNos($nos){
  switch($nos){
    case"०":return 0;
    case"१":return 1;
    case"२":return 2;
    case"३":return 3;
    case"४":return 4;
    case"५":return 5;
    case"६":return 6;
    case"७":return 7;
    case"८":return 8;
    case"९":return 9;
    case"0":return"०";
    case"1":return"१";
    case"2":return"२";
    case"3":return"३";
    case"4":return"४";
    case"5":return"५";
    case"6":return"६";
    case"7":return"७";
    case"8":return"८";
    case"9":return"९";
   }
}

Can someone help me solve this?

May 28, 2022 in PHP by Kichu
• 19,040 points
693 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Try this :

/* Set internal character encoding to UTF-8 */
   header('Content-Type: text/html; charset=utf-8');
   mb_internal_encoding("UTF-8");

// An array of Nepali number representations
function convertNos($nos){
    $n = '';
  switch($nos){
    case "०": $n = 0; break;
    case "१": $n = 1; break;
    case "२": $n= 2; break;
    case "३": $n = 3; break;
    case "४": $n = 4; break;
    case "५": $n = 5; break;
    case "६": $n = 6; break;
    case "७": $n = 7; break;
    case "८": $n = 8; break;
    case "९": $n = 9; break;
    case "0": $n = "०"; break;
    case "1": $n = "१"; break;
    case "2": $n = "२"; break;
    case "3": $n = "३"; break;
    case "4": $n = "४"; break;
    case "5": $n = "५"; break;
    case "6": $n = "६"; break;
    case "7": $n = "७"; break;
    case "8": $n = "८"; break;
    case "9": $n = "९"; break;
   }
   return $n;
}

$num = 0; // get your number
// replace this with whatever you're using to get your number
if (isset($_GET['number'])) $num = strip_tags($_GET['number']); 
/* Convert your number (could be a string of unicode, 
 * not necessarily a digit) into a string and split it
 * to get an array of characters. 
 */
 $str_num = preg_split('//u', ("". $num), -1); // not explode('', ("". $num))

    // For each item in your exploded string, retrieve the Nepali equivalent or vice versa.
    $out = '';
    $out_arr = array_map('convertNos', $str_num);
    $out = implode('', $out_arr);
    print($out);
    // Also make sure your PHP file is saved as a UTF-8 text file

It worked for me. I hope this helps you.

answered May 29, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In PHP

0 votes
1 answer

How to change the session timeout in PHP?

Hello @kartik, Put $_SESSION['login_time'] = time(); into the previous authentication ...READ MORE

answered Sep 15, 2020 in PHP by Niroj
• 82,840 points
4,529 views
0 votes
1 answer

How to change php version in htaccess in server?

Hello, To switch to PHP 4.4: AddHandler application/x-httpd-php4 .php To ...READ MORE

answered Nov 4, 2020 in PHP by Niroj
• 82,840 points
3,024 views
0 votes
1 answer

How do I convert a string to a number in PHP?

You don't have to do this, since ...READ MORE

answered Feb 23, 2022 in PHP by Aditya
• 7,680 points
682 views
0 votes
1 answer

How to merge two arrays while keeping keys instead of reindexing in php?

Hello, Considering that you have $replaced = array('1' => ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,840 points
3,179 views
0 votes
1 answer

How to get the client IP address in PHP ?

Hello, Here is a code sample of a good ...READ MORE

answered Apr 8, 2020 in PHP by Niroj
• 82,840 points
7,143 views
0 votes
1 answer

How to implement a callback in PHP?

Hello, Implementation of a callback is done like ...READ MORE

answered Apr 15, 2020 in PHP by Niroj
• 82,840 points
1,142 views
0 votes
1 answer

PHP Convert String to SEO Friendly Url For Bengali Language Type

$string = preg_replace("/[^a-z0-9]/u", "$separator", $string);  change the ...READ MORE

answered Feb 14, 2022 in Others by narikkadan
• 63,600 points
1,229 views
0 votes
0 answers

Laravel/ MSSQL (AWS RDS) General error: 20018 Unicode data

I'm using SQL Server with a Laravel ...READ MORE

Apr 25, 2022 in AWS by Aditya
• 7,680 points
1,294 views
0 votes
0 answers

Google Unicode Fonts not rendered properly in mpdf for PHP

I want to use Baloo2 Google Font ...READ MORE

Jun 9, 2022 in PHP by Kichu
• 19,040 points
1,983 views
+1 vote
2 answers

Scp Php files into server using gradle

Tru something like this: plugins { id ...READ MORE

answered Oct 11, 2018 in DevOps & Agile by lina
• 8,220 points
1,831 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP