Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Funkcja czytania ciągów liczb
Forum PHP.pl > Forum > Przedszkole
www.aukcje.fm
Witam,

Jak można napisać prostą funkcję która ciąg np:

123

przekształciła na słowa czyli:

jeden dwa trzy

?


I aby czytała również po trzy czyli sto dwadzieścia trzy.

A ciągi znaków mam np 600401402 czyli numery telefonów.

dla pojedyńczych znalazałem coś takiego


  1. <?php
  2. $a = array(1, 2, 3, 4, 5);
  3. $b = array("one", "two", "three", "four", "five");
  4. $c = array("uno", "dos", "tres", "cuatro", "cinco");
  5. $d = array("jeden", "dwa", "trzy", "cztery", "pięć");
  6.  
  7. $e = array_map(null, $a, $b, $c, $d);
  8. print_r($e);
  9. ?>


Ale jak czytać tym ciągi liczb oraz jak czytać po trzy.

Będę miał zmienną języka również.

Jeszcze jest coś takiego:

  1. function pokaz_po_Hiszpansku($n, $m)
  2. {
  3. return "Po Hiszpańsku liczba $n to $m";
  4. }
  5.  
  6. function mapuj_na_Hiszpanski($n, $m)
  7. {
  8. return array ($n => $m);
  9. }
  10.  
  11. $a = array(1, 2, 3, 4, 5);
  12. $b = array("uno", "dos", "tres", "cuatro", "cinco");
  13.  
  14. $c = array_map("pokaz_po_Hiszpansku", $a, $b);
  15. print_r($c);
  16.  
  17. $d = array_map("mapuj_po_Hiszpansku", $a , $b);
  18. print_r($d);
Randallmaster
Z wykorzystaniem twoich przykładów

  1. $string = 123;
  2. $var = str_split($string, 1);
  3.  
  4. function convert($n, $m)
  5. {
  6. return $m;
  7. }
  8.  
  9. $b = array("jeden", "dwa", "trzy");
  10.  
  11. $c = array_map("convert", $var, $b);
  12. print_r($c);
www.aukcje.fm
Ok zrobiłem tak ;

  1. <?php
  2.  
  3. class pl {
  4.  
  5. static function convert($number) {
  6. // check if number is negative
  7. $negative = false;
  8. if($number < 0) {
  9. $negative = true;
  10. $number = abs($number); // turn to positive
  11. }
  12. if($number == 0) { // if zero return zero
  13. return 'zero';
  14. }
  15. $i = -1; // our numberMap key
  16. $result = '';
  17. while($number >= 1) {
  18. $token = $number % 1000; // get 3 digits
  19. $number = ($number - $token) / 1000; // cut the number
  20. if($i >= 0) { // if numberMap key is greater than equal thousands
  21. list($first, $second, $third) = self::$numberMap[$i]; // get plural values from numberMap
  22. $pluralize = self::pluralize($token, $first, $second, $third); // pluralize
  23. } else {
  24. $pluralize = '';
  25. }
  26. if($token != 0) { // for zero we don't write anything to output
  27. $hundredsOf = self::hundredsOf($token) . ' '; // convert 3 digit token
  28. $result = $hundredsOf . $pluralize . ' ' . $result ; // add to output string
  29. }
  30. $i++;
  31. }
  32. return trim($negative ? 'minus ' . $result : $result);
  33. }
  34.  
  35.  
  36. static function pluralize($number, $first, $second, $third) {
  37. $number = abs($number); // get absolute value, for negative numbers algoritm is the same
  38. if($number > 20) { // if number is greater than 20
  39. $number %= 10; // get the last digit
  40. if($number == 1) { // for 21, 31, 41, ... result is the same as for 0
  41. $number--;
  42. }
  43. }
  44. if($number == 1) { // 1 - first case
  45. return $first;
  46. } else if($number >= 2 && $number <= 4) { // 2,3,4 - second case
  47. return $second;
  48. } else { // 0,6,7,8,9 - third case
  49. return $third;
  50. }
  51. }
  52.  
  53. protected static $numberMap = array(
  54. array('tysiąc', 'tysiące', 'tysięcy'),
  55. array('milion', 'miliony', 'milionów'),
  56. array('miliard', 'miliardy', 'miliardów'),
  57. array('bilion', 'biliony', 'bilionów'),
  58. array('biliard', 'biliardy', 'biliardów'),
  59. array('trylion', 'tryliony', 'trylionów'),
  60. array('tryliard', 'tryliardy', 'tryliardów')
  61. );
  62.  
  63. protected static $ones = array(
  64. 'jeden', 'dwa', 'trzy',
  65. 'cztery', 'pięć', 'sześć',
  66. 'siedem', 'osiem', 'dziewięć'
  67. );
  68.  
  69. protected static $tens = array(
  70. 'dziesięć', 'dwadzieścia', 'trzydzieści',
  71. 'czterdzieści', 'pięćdziesiąt', 'sześćdziesiąt',
  72. 'siedemdziesiąt', 'osiemdziesiąt', 'dziewięćdziesiąt'
  73. );
  74.  
  75. protected static $specialTens = array(
  76. 'jedenaście', 'dwanaście', 'trzynaście',
  77. 'czternaście', 'piętnaście', 'szesnaście',
  78. 'siedemnaście', 'osiemnaście', 'dziewiętnaście'
  79. );
  80.  
  81. protected static $hundreds = array(
  82. 'sto', 'dwieście', 'trzysta',
  83. 'czterysta', 'pięćset', 'sześćset',
  84. 'siedemset', 'osiemset', 'dziewięćset'
  85. );
  86.  
  87. protected static function hundredsOf($number) {
  88. $ones = $number % 10;
  89. $tens = (($number % 100) - $ones);
  90. $hundreds = ($number - $tens - $ones) / 100;
  91. $tens /= 10;
  92.  
  93. $result = '';
  94. if($hundreds != 0) {
  95. $result .= self::$hundreds[$hundreds - 1] . ' ';
  96. }
  97. if($tens == 1 && $ones != 0) {
  98. $result .= self::$specialTens[$ones - 1] . ' ';
  99. } else {
  100. if($tens != 0) {
  101. $result .= self::$tens[$tens - 1] . ' ';
  102. }
  103. if($ones != 0) {
  104. $result .= self::$ones[$ones - 1] . ' ';
  105. }
  106. }
  107.  
  108. return trim($result);
  109. }
  110. }
  111. ?>
  112.  
  113. <?php echo pl::convert($numer);?>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.