ท่านไหนมีโค๊ดแปลงจากตัวเลขเป็นข้อความไหมครับ

wave
Nattawuth Singhata
1,915
สังคมนักพัฒนาระบบ (Community)
ท่านไหนมีโค๊ดแปลงจากตัวเลขเป็นข้อความไหมครับ

<p>ตัวอย่างเช่น 123 เป็น หนึ่งร้อยยี่สิบสามบาทถ้วนครับ</p>

2 ความคิดเห็น

  • มานพ กองอุ่น

  • Nattawuth Singhata

ตัวอย่างเช่น 123 เป็น หนึ่งร้อยยี่สิบสามบาทถ้วนครับ

	function tothai ($num = 0) {

		// split satang
	$floor = floor($num);
	
	$satang = $num - $floor;
	
	if ($satang > 0) { $satang = floor($satang * 100);  }
	
	$num = $floor;
	
	//
	
	$thai = "";
	
	$len = strlen($num);
	
	$tnum = split(" ","ศูนย์ หนึ่ง สอง สาม สี่ ห้า หก เจ็ด แปด เก้า"); 
	
	$tnth = split(" ","สิบ ร้อย พัน หมื่น แสน ล้าน สิบล้าน ร้อยล้าน พันล้าน แสนล้าน หมื่นล้าน ล้านล้าน");
	
	//
	
	foreach (str_split($num) as $k => $v) {
	
	$nth = $len - $k;
	
	if ($nth == 1 and $v == 1) { 
	
	$thai .= "เอ็ด"; 
	
	} elseif ($nth == 1 and $v == 0) {  
	
	$thai .= "";
	
	} else {
	
	$thai .= $tnum[$v];
	
	}
	
	$thai .= $tnth[$nth-2];
	
	}
	
	$thai .= "บาท";
	
	// SATANG
	
	if ($satang) { 
	
	foreach (str_split($satang) as $k => $v) {
	
	$nth = 2 - $k;
	
	if ($nth == 1 and $v == 1) { 
	
	$thai .= "เอ็ด"; 
	
	} elseif ($nth == 1 and $v == 0) {  
	
	$thai .= "";
	
	} else {
	
	$thai .= $tnum[$v];
	
	}
	
	$thai .= $tnth[$nth-2];
	
	}
	
	$thai .= "สตางค์";
	
	} else {
	
	$thai .= "ถ้วน";
	
	}
	
	// $thai .= "<br/>".$num . " ". $satang;
	
	return $thai;
	
	}

ที่มา http://lordbsd.blogspot.com/2013/07/php-bahttext.html