如何在php中将英文数字更改为尼泊尔数字

时间:2014-03-10 07:44:26

标签: php unicode

在我的项目中,我必须将英文数字更改为尼泊尔数字,最多2位数。例如如果我输入1它应该返回1并且如果我输入41它应该返回41并且我必须在数据库中存储41并且在前端显示它。我该怎么做?我试着用“font-family:Preeti;”获得尼泊尔数字但是它给出了¥¥而不是1。同样,当我使用下面的功能时,它会给出¥¥而不是1。我怎么解决这个问题?

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"९";
   }
}

欢迎任何帮助/建议。谢谢。

2 个答案:

答案 0 :(得分:1)

/* 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

答案 1 :(得分:0)

尝试使用utf-8编码。

html:<meta charset="utf-8" />

php:header('Content-Type: text/html; charset=utf-8');