Hàm php chuyển số sang chữ (đánh vần chữ số)
function numberToString($a) { $result = ""; // please write your code here $a = (string) $a; $leng = strlen($a); if ($leng == 1) { return $map[$a]; } $convert = chunkStringToArray($a); $count = count($convert); $j = $count; for ($i = 0; $i < $count; $i++) { if ($convert[$i] == '000') { $j--; continue; } if ($j % 4 == 0) { $result .= " " . numberToThousand($convert[$i]) . " ty"; } else if ($j % 3 == 0) { $result .= " " . numberToThousand($convert[$i]) . " trieu"; } else if ($j % 2 == 0) { $result .= " " . numberToThousand($convert[$i]) . " nghin"; } else { $result .= " " . numberToThousand($convert[$i]); } $j--; } //replace if end number is 4 $result = trim($result); if (substr($result, -3) == 'bon') { $result = substr($result, 0, -3) . 'tu'; } return $result; } function numberToThousand($a) { $a = (string) $a; $map = ["0" => "khong", "1" => "mot", "2" => "hai", "3" => "ba", "4" => "bon", "5" => "nam", "6" => "sau", "7" => "bay", "8" => "tam", "9" => "chin"]; $leng = strlen($a); $str = ''; $j = $leng; if ($a == '10') return 'muoi'; if ($leng == 2 && $a[0] == '1') return trim("muoi {$map[$a[1]]}"); for ($i = 0; $i < $leng; $i++) { if ($j == 3) { $str .= " {$map[$a[$i]]} tram"; } else if ($j == 2) { if ($a[$i] == '0' && $leng == 3 && $a[2] != '0') $str .= ' linh'; else if ($a[$i] != '0') $str .= " {$map[$a[$i]]} muoi"; } else { if ($a[$i] != '0') $str .= " {$map[$a[$i]]} "; } $j--; } return trim($str); } function chunkStringToArray($a) { $a = number_format((int) $a, 0); return explode(',', $a); } echo numberToString($_GET['n']);