├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── src ├── Word.php └── helpers.php └── tests └── WordTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | vendor/autoload.php 3 | vendor/ 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Saiful Islam 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # number-to-word 2 | এটি একটি পিএইচপি ক্লাস যা সংখ্যাকে কথায় পরিনত করে। 3 | 4 | [Live Demo/এখানে ডেমো দেখুন](https://saiful.im/n2w.php) 5 | 6 | ## Installation 7 | 8 | ``` 9 | composer require saaiful/number-to-word 10 | ``` 11 | or 12 | ``` 13 | { 14 | "require": { 15 | "saaiful/bangla-number-word": "0.*" 16 | } 17 | } 18 | ``` 19 | 20 | ## Usage 21 | ```php 22 | ... 23 | require 'vendor/autoload.php'; 24 | use Saaiful\NumberToWord\Word; 25 | 26 | $bangla = new Word(324650); 27 | 28 | echo $bangla->word(); 29 | ``` 30 | or 31 | ```php 32 | ... 33 | require 'vendor/autoload.php'; 34 | use Saaiful\NumberToWord\Word; 35 | 36 | $bangla = new Word('-১২৩'); 37 | 38 | echo $bangla->word(); 39 | ``` 40 | or 41 | 42 | ```php 43 | ... 44 | require 'vendor/autoload.php'; 45 | echo n2w($number); 46 | ``` 47 | 48 | ## Without Composer 49 | Download `Word.php` and `helper.php` from the repo and save the file into your project path somewhere. 50 | ```php 51 | word(); 60 | ``` 61 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "saaiful/number-to-word", 3 | "description": "Numbers to Words Converter (Bangla)", 4 | "keywords": ["bangla", "word", "number", "converter"], 5 | "homepage": "https://github.com/saaiful/number-to-word", 6 | "license": "MIT", 7 | "authors": [{ 8 | "name": "Saiful Isalm", 9 | "email": "me@saiful.im" 10 | }], 11 | "require": { 12 | "php": ">=5.5.0" 13 | }, 14 | "require-dev": { 15 | "phpunit/phpunit": "^4.8 || ^5.0" 16 | }, 17 | "autoload": { 18 | "files": ["src/helpers.php"], 19 | "psr-4": { 20 | "Saaiful\\NumberToWord\\": "src/" 21 | } 22 | }, 23 | "autoload-dev": { 24 | "files": ["src/helpers.php"], 25 | "psr-4": { 26 | "Saaiful\\NumberToWord\\Tests\\": "tests/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Word.php: -------------------------------------------------------------------------------- 1 | dictionary = [ 23 | 0 => "শূন্য", 24 | 1 => "এক", 25 | 2 => "দুই", 26 | 3 => "তিন", 27 | 4 => "চার", 28 | 5 => "পাঁচ", 29 | 6 => "ছয়", 30 | 7 => "সাত", 31 | 8 => "আট", 32 | 9 => "নয়", 33 | 10 => "দশ", 34 | 11 => "এগারো", 35 | 12 => "বারো", 36 | 13 => "তেরো", 37 | 14 => "চৌদ্দ", 38 | 15 => "পনের", 39 | 16 => "ষোল", 40 | 17 => "সতের", 41 | 18 => "আঠার", 42 | 19 => "উনিশ", 43 | 20 => "বিশ", 44 | 21 => "একুশ", 45 | 22 => "বাইশ", 46 | 23 => "তেইশ", 47 | 24 => "চব্বিশ", 48 | 25 => "পঁচিশ", 49 | 26 => "ছাব্বিশ", 50 | 27 => "সাতাশ", 51 | 28 => "আটাশ", 52 | 29 => "ঊনত্রিশ", 53 | 30 => "ত্রিশ", 54 | 31 => "একত্রিশ", 55 | 32 => "বত্রিশ", 56 | 33 => "তেত্রিশ", 57 | 34 => "চৌত্রিশ", 58 | 35 => "পঁয়ত্রিশ", 59 | 36 => "ছত্রিশ", 60 | 37 => "সাইত্রিশ", 61 | 38 => "আটত্রিশ", 62 | 39 => "ঊনচল্লিশ", 63 | 40 => "চল্লিশ", 64 | 41 => "একচল্লিশ", 65 | 42 => "বিয়াল্লিশ", 66 | 43 => "তেতাল্লিশ", 67 | 44 => "চুয়াল্লিশ", 68 | 45 => "পঁয়তাল্লিশ", 69 | 46 => "ছেচল্লিশ", 70 | 47 => "সাতচল্লিশ", 71 | 48 => "আটচল্লিশ", 72 | 49 => "ঊনপঞ্চাশ", 73 | 50 => "পঞ্চাশ", 74 | 51 => "একান্ন", 75 | 52 => "বাহান্ন", 76 | 53 => "তেপ্পান্ন", 77 | 54 => "চুয়ান্ন", 78 | 55 => "পঞ্চান্ন", 79 | 56 => "ছাপ্পান্ন", 80 | 57 => "সাতান্ন", 81 | 58 => "আটান্ন", 82 | 59 => "ঊনষাট", 83 | 60 => "ষাট", 84 | 61 => "একষট্টি", 85 | 62 => "বাষট্টি", 86 | 63 => "তেষট্টি", 87 | 64 => "চৌষট্টি", 88 | 65 => "পঁয়ষট্টি", 89 | 66 => "ছেষট্টি", 90 | 67 => "সাতষট্টি", 91 | 68 => "আটষট্টি", 92 | 69 => "ঊনসত্তর", 93 | 70 => "সত্তর", 94 | 71 => "একাত্তর", 95 | 72 => "বাহাত্তর", 96 | 73 => "তিয়াত্তর", 97 | 74 => "চুয়াত্তর", 98 | 75 => "পঁচাত্তর", 99 | 76 => "ছিয়াত্তর", 100 | 77 => "সাতাত্তর", 101 | 78 => "আটাত্তর", 102 | 79 => "ঊনআশি", 103 | 80 => "আশি", 104 | 81 => "একাশি", 105 | 82 => "বিরাশি", 106 | 83 => "তিরাশি", 107 | 84 => "চুরাশি", 108 | 85 => "পঁচাশি", 109 | 86 => "ছিয়াশি", 110 | 87 => "সাতাশি", 111 | 88 => "অষ্টআশি", 112 | 89 => "ঊননব্বই", 113 | 90 => "নব্বই", 114 | 91 => "একানব্বই", 115 | 92 => "বিরানব্বই", 116 | 93 => "তিরানব্বই", 117 | 94 => "চুরানব্বই", 118 | 95 => "পঁচানব্বই", 119 | 96 => "ছিয়ানব্বই", 120 | 97 => "সাতানব্বই", 121 | 98 => "আটানব্বই", 122 | 99 => "নিরানব্বই", 123 | 100 => "শত", 124 | 1000 => "হাজার", 125 | 100000 => "লক্ষ", 126 | 10000000 => "কোটি", 127 | ]; 128 | $this->number = $this->b2e(str_replace([' ', ','], '', $number)); 129 | } 130 | 131 | /** 132 | * Convert Number from Bangla to English 133 | * @param string $number 134 | * @return string 135 | */ 136 | private function b2e($number) 137 | { 138 | $en = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; 139 | $bn = ['০', '১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯']; 140 | return str_replace($bn, $en, $number); 141 | } 142 | 143 | /** 144 | * Decimal Processing 145 | * @param string $number 146 | * @return string 147 | */ 148 | private function decimal($number) 149 | { 150 | $string = []; 151 | foreach (str_split($number) as $key => $value) { 152 | $string[] = $this->dictionary[(int) $value]; 153 | } 154 | return implode(" ", $string); 155 | } 156 | 157 | /** 158 | * Number to Word Separator (Main Logic) 159 | * @param integer $number 160 | * @return string 161 | */ 162 | private function separator($number) 163 | { 164 | $range = [ 165 | 100 => 0, 166 | 1000 => 100, 167 | 100000 => 1000, 168 | 10000000 => 100000, 169 | 1000000000000000000 => 10000000, 170 | ]; 171 | 172 | $string = ''; 173 | 174 | if ($number < 100) { 175 | $string = $this->dictionary[$number]; 176 | } else { 177 | foreach ($range as $divisor => $previousDivisor) { 178 | if ($number < $divisor) { 179 | $units = explode('.', $number / $previousDivisor); 180 | $left = $number % $previousDivisor; 181 | $space = ($previousDivisor == 100) ? '' : ' '; 182 | if ($left > 0) { 183 | $string = $this->separator($units[0]) . $space . $this->dictionary[$previousDivisor] . ' ' . $this->separator($left); 184 | } else { 185 | $string = $this->separator($units[0]) . $space . $this->dictionary[$previousDivisor]; 186 | } 187 | break; 188 | } 189 | } 190 | } 191 | 192 | // Handle the case where the number is too large to be processed 193 | if ($string === '') { 194 | $string = 'এই সংখ্যা কথায় প্রকাশ করা যাচ্ছে না'; 195 | } 196 | 197 | return $string; 198 | } 199 | 200 | /** 201 | * Numbrt to Word 202 | * @return string 203 | */ 204 | public function word() 205 | { 206 | $string = ''; 207 | if ($this->number < 0) { 208 | $string = $this->negative; 209 | $this->number = abs($this->number); 210 | } 211 | $number = explode('.', $this->number); 212 | if (count($number) == 1) { 213 | $string .= $this->separator($number[0]); 214 | } elseif (count($number) == 2) { 215 | $string .= $this->separator($number[0]) . $this->decimal . $this->decimal($number[1]); 216 | } 217 | if (preg_match("/^ (লক্ষ|কোটি|শত|হাজার)/", $string)) { 218 | $string = 'এক' . $string; 219 | } 220 | return trim(str_replace(" ", '', $string)); 221 | } 222 | } 223 | -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 | word(); 11 | } 12 | -------------------------------------------------------------------------------- /tests/WordTest.php: -------------------------------------------------------------------------------- 1 | assertEquals('দশ', $test->word()); 12 | 13 | $test = new Word(40); 14 | $this->assertEquals('চল্লিশ', $test->word()); 15 | 16 | $test = new Word(99); 17 | $this->assertEquals('নিরানব্বই', $test->word()); 18 | } 19 | 20 | public function testNinetyNineToManyCr() 21 | { 22 | $test = new Word(1999); 23 | $this->assertEquals('এক হাজার নয়শত নিরানব্বই', $test->word()); 24 | 25 | $test = new Word(10000); 26 | $this->assertEquals('দশ হাজার', $test->word()); 27 | 28 | $test = new Word(1234567890); 29 | $this->assertEquals('একশত তেইশ কোটি পঁয়তাল্লিশ লক্ষ সাতষট্টি হাজার আটশত নব্বই', $test->word()); 30 | 31 | $test = new Word(100000); 32 | $this->assertEquals('এক লক্ষ', $test->word()); 33 | 34 | $test = new Word(10000000); 35 | $this->assertEquals('এক কোটি', $test->word()); 36 | } 37 | 38 | public function testBanglaUnicode() 39 | { 40 | $test = new Word('১০'); 41 | $this->assertEquals('দশ', $test->word()); 42 | } 43 | 44 | public function testNegative() 45 | { 46 | $test = new Word(-10); 47 | $this->assertEquals('ঋণাত্মক দশ', $test->word()); 48 | } 49 | 50 | public function testDecimal() 51 | { 52 | $test = new Word(10.55); 53 | $this->assertEquals('দশ দশমিক পাঁচ পাঁচ', $test->word()); 54 | 55 | $test = new Word(10.01); 56 | $this->assertEquals('দশ দশমিক শূন্য এক', $test->word()); 57 | } 58 | 59 | public function testSeparator() 60 | { 61 | $test = new Word('10,000'); 62 | $this->assertEquals('দশ হাজার', $test->word()); 63 | 64 | $test = new Word('10 000'); 65 | $this->assertEquals('দশ হাজার', $test->word()); 66 | } 67 | 68 | public function testHelper() 69 | { 70 | $this->assertEquals('দশ হাজার', n2w(10000)); 71 | } 72 | 73 | public function testMinMaxRange() 74 | { 75 | $this->assertEquals('শূন্য', n2w(0)); 76 | $this->assertEquals('এই সংখ্যা কথায় প্রকাশ করা যাচ্ছে না', n2w(1000000000000000000)); 77 | } 78 | } 79 | --------------------------------------------------------------------------------