ホームページ  >  記事  >  バックエンド開発  >  アラビア数字を漢字(大文字、小文字)に変換します。

アラビア数字を漢字(大文字、小文字)に変換します。

WBOY
WBOYオリジナル
2016-07-25 09:01:441613ブラウズ
数字を漢字に変換するには
自分で書きました
  1. 関数番号2 Chinese($num, $m = 1) {
  2. switch($m) {
  3. case 0:
  4. $CNum = array(
  5. array('zero','壹','二','三','四','五','鲁','旒','捌','玖'),
  6. array('','十','百','千'),
  7. array(' ','百万','十億','兆')
  8. );
  9. ブレーク;
  10. デフォルト:
  11. $CNum = array(
  12. array('zero','one','two','three',' four ','five','six','seven','eight','nine'),
  13. array('','ten','hundred','thousand'),
  14. array('',' ワン','billion','trillion')
  15. );
  16. Break;
  17. }
  18. // $cNum = array('zero','one','two','three','four',' four' ,'6','7','8','9');
  19. if (is_integer($num)) {
  20. $int = (string)$num;
  21. } else if (is_numeric($num )) {
  22. $num =explode('.', (string)floatval($num));
  23. $int = $num[0];
  24. $fl = isset($num[1]) : FALSE;
  25. }
  26. // 長さ
  27. $len = strlen($int);
  28. // 中国語
  29. $chinese = array();
  30. // 逆数
  31. $str = strrev($int );
  32. for($i = 0; $i $s = array(0=>$str[$i], 1=>$str[$i+ 1 ], 2= >$str[$i+2], 3=>$str[$i+3]);
  33. $j = '';
  34. // 千の位
  35. if ($s[3 ] !== ' ') {
  36. $s[3] = (int) $s[3];
  37. if ($s[3] !== 0) {
  38. $j .= $CNum[0][$ s [3]] .$CNum[1][3];
  39. } else {
  40. if ($s[2] != 0 || $s[1] != 0 || $s[0]!=0 ) {
  41. $j .= $CNum[0][0];
  42. }
  43. }
  44. }
  45. // 百の位
  46. if ($s[2] !== '') {
  47. $s[2] = ( int ) $s[ 2];
  48. if ($s[2] !== 0) {
  49. $j .= $CNum[0][$s[2]].$CNum[1][2];
  50. } else {
  51. if ($s[3]!=0 && ($s[1] != 0 || $s[0]!=0) ) {
  52. $j .= $CNum[0][0]
  53. }
  54. }
  55. }
  56. // 十の位
  57. if ($s[1] !== '') {
  58. $s[1] = (int) $s[1];
  59. if ($s[1 ] !== 0 ) {
  60. $j .= $CNum[0][$s[1]].$CNum[1][1];
  61. } else {
  62. if ($s[0]!=0 && $ s[2] != 0) {
  63. $j .= $CNum[0][$s[1]];
  64. }
  65. }
  66. }
  67. // 単位桁
  68. if ($s[0] !== ' ') {
  69. $s[0] = (int) $s[0];
  70. if ($s[0] !== 0) {
  71. $j .= $CNum[0][$s[0] ] .$CNum[ 1][0];
  72. } else {
  73. // $j .= $CNum[0][0];
  74. }
  75. }
  76. $j.=$CNum[2][$ i /4];
  77. array_unshift($chinese, $j);
  78. }
  79. $chs = implode('', $chinese);
  80. if ($fl) {
  81. $chs .= 'point';
  82. for ( $i=0 ,$j=strlen($fl); $i<$j; $i++) {
  83. $t = (int)$fl[$i];
  84. $chs.= $str[0][ $t];
  85. }
  86. }
  87. return $chs;
  88. }
コードをコピー
  1. 関数番号2 Chinese($num, $m = 1) {
  2. switch($m) {
  3. case 0:
  4. $CNum = array(
  5. array('zero','壹','二','三','四','五','鲁','旒','捌','玖'),
  6. array('','十','百','千'),
  7. array(' ','百万','十億','兆')
  8. );
  9. ブレーク;
  10. デフォルト:
  11. $CNum = array(
  12. array('zero','one','two','three',' four ','five','six','seven','eight','nine'),
  13. array('','ten','hundred','thousand'),
  14. array('',' ワン','billion','trillion')
  15. );
  16. Break;
  17. }
  18. // $cNum = array('zero','one','two','three','four',' four' ,'6','7','8','9');
  19. if (is_integer($num)) {
  20. $int = (string)$num;
  21. } else if (is_numeric($num )) {
  22. $num =explode('.', (string)floatval($num));
  23. $int = $num[0];
  24. $fl = isset($num[1]) : FALSE;
  25. }
  26. // 長さ
  27. $len = strlen($int);
  28. // 中国語
  29. $chinese = array();
  30. // 逆数
  31. $str = strrev($int );
  32. for($i = 0; $i<$len; $i+=4 ) {
  33. $s = array(0=>$str[$i], 1=>$str[$i+ 1 ], 2= >$str[$i+2], 3=>$str[$i+3]);
  34. $j = '';
  35. // 千の位
  36. if ($s[3 ] !== ' ') {
  37. $s[3] = (int) $s[3];
  38. if ($s[3] !== 0) {
  39. $j .= $CNum[0][$ s [3]] .$CNum[1][3];
  40. } else {
  41. if ($s[2] != 0 || $s[1] != 0 || $s[0]!=0 ) {
  42. $j .= $CNum[0][0];
  43. }
  44. }
  45. }
  46. // 百の位
  47. if ($s[2] !== '') {
  48. $s[2] = ( int ) $s[ 2];
  49. if ($s[2] !== 0) {
  50. $j .= $CNum[0][$s[2]].$CNum[1][2];
  51. } else {
  52. if ($s[3]!=0 && ($s[1] != 0 || $s[0]!=0) ) {
  53. $j .= $CNum[0][0]
  54. }
  55. }
  56. }
  57. // 十の位
  58. if ($s[1] !== '') {
  59. $s[1] = (int) $s[1];
  60. if ($s[1 ] !== 0 ) {
  61. $j .= $CNum[0][$s[1]].$CNum[1][1];
  62. } else {
  63. if ($s[0]!=0 && $ s[2] != 0) {
  64. $j .= $CNum[0][$s[1]];
  65. }
  66. }
  67. }
  68. // 単位桁
  69. if ($s[0] !== ' ') {
  70. $s[0] = (int) $s[0];
  71. if ($s[0] !== 0) {
  72. $j .= $CNum[0][$s[0] ] .$CNum[ 1][0];
  73. } else {
  74. // $j .= $CNum[0][0];
  75. }
  76. }
  77. $j.=$CNum[2][$ i /4];
  78. array_unshift($chinese, $j);
  79. }
  80. $chs = implode('', $chinese);
  81. if ($fl) {
  82. $chs .= 'point';
  83. for ( $i=0 ,$j=strlen($fl); $i<$j; $i++) {
  84. $t = (int)$fl[$i];
  85. $chs.= $str[0][ $t];
  86. }
  87. }
  88. return $chs;
  89. }
コードをコピー


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。