7
яну
2010
PHP-конвертиране на число в дума
Много полезен PHP клас за конвертиране на число в дума – по нета има и други скриптове но този членува правилно думите на български .
class TextualNumber
{
var $units = array();
var $teens = array();
var $hundredth = array();
var $tens = array();
var $suffix = array();
function TextualNumber()
{
$this->units = array('нула', 'едно', 'две', 'три', 'четери', 'пет', 'шест', 'седем', 'осем',
'девет');
$this->teens = array('десет', 'единадесет', 'дванадесет', 'тринадесет',
'четеринадесет', 'петнадесет', 'шестнадесет', 'седемнадесет',
'осемнадесет', 'деветнадесет');
$this->hundredth =
array(1=>'сто','двеста','триста','четеристотин','петстотин','шестстотин','седемстотин',
'осемстотин','деветстотин');
$this->tens = array(2 => 'двадесет', 'тридесет', 'четеридесет', 'петдесет',
'шестдесет', 'седемдесет', 'осемдесет', 'деветдесет');
$this->suffix = array('хиляди', 'милиона', 'милиарда', 'trillion', 'quadrillion');
}
/*
* @param string $int The number to convert. Optional.
* @return text Resulting text
*/
function ToString($int)
{
// Check for purely numeric chars
if (!preg_match('#^[\d.]+$#', $int)) {
echo('Невалидни символи! Моля въведете числова стойност.');
return;
}
// Handle decimals
if (strpos($int, '.') !== false) {
$decimal = substr($int, strpos($int, '.') + 1);
$int = substr($int, 0, strpos($int, '.'));
}
// Lose insignificant zeros
$int = ltrim($int, '0');
// Check for valid number
if ($int == '') {
$int = '0';
}
// Lose the negative, don't use abs() so as to allow large numbers
if ($negative = ($int < 0)) {
$int = substr($int, 1);
}
// Number too big?
if (strlen($int) > 18) {
//throw new Exception('Out of range');
echo('Числото е повече от 18 символа!');
return;
}
// Keep original number
$orig = $int;
/**
* Main number deciphering bit thing
*/
switch (strlen($int)) {
// Single digit number
case '1':
$text = $this->units[$int];
break;
// Two digit number
case '2':
if ($int{0} == '1') {
$text = $this->teens[$int{1}];
} else if ($int{1} == '0') {
$text = $this->tens[$int{0}];
if($this->flag == 3) $text = 'и '.$text;
echo $this->flag."";
$this->flag = 0;
} else {
$text = $this->tens[$int{0}] . ' и ' . $this->units[$int{1}];
}
break;
// Three digit number
case '3':
if ($int % 100 == 0) {
$text = $this->hundredth[$int{0}];
} else {
$int_tmp = substr($int, 1);
if($int_tmp{0} == '0' || $int_tmp{0} == '1') $add = 'и';
$this->flag = 3;
$text = $this->hundredth[$int{0}] . " $add " . $this->GetText(substr($int, 1));
}
break;
// Anything else
default:
$pieces = array();
$suffixIndex = 0;
// Handle the last three digits
$num = substr($int, -3);
if ($num > 0) {
$pieces[] = $this->GetText($num);
}
$int = substr($int, 0, -3);
// Now handle the thousands/millions etc
while (strlen($int) > 3) {
$num = substr($int, -3);
if ($num > 0) {
$pieces[] = $this->GetText($num) . ' ' . $this->suffix[$suffixIndex];
}
$int = substr($int, 0, -3);
$suffixIndex++;
}
if (substr($int, -3) == 1)
{
$t = $this->suffix[$suffixIndex];
if($suffixIndex == 0 && $int == 1) $ending = 'а';
else $preff = 'един ';
$pieces[] = $preff . substr($t,0,-2) . $ending;
}
else
{
$pieces[] = $this->GetText($int) . ' ' . $this->suffix[$suffixIndex];
}
/**
* Figure out whether we need to add "and" in there somewhere
*/
$pieces = array_reverse($pieces);
if (count($pieces) > 1 AND strpos($pieces[count($pieces) - 1], ' и ') === false) {
$pieces[] = $pieces[count($pieces) - 1];
$pieces[count($pieces) - 2] = 'и';
}
// Create the text
$text = implode(' ', $pieces);
// Negative number?
if ($negative) {
$text = 'минус ' . $text;
}
break;
}
/**
* Handle any decimal part
*/
if (!empty($decimal)) {
$pieces = array();
$decimal = preg_replace('#[^0-9]#', '', $decimal);
/*
for ($i=0, $len=strlen($decimal); $i<$len; ++$i) {
$pieces[] = $this->teens[$decimal{$i}];
}
*/
if(strlen($decimal) == '1')
{
$text .= ' лева и ' . $this->tens[$decimal{0}];
$text .= ' стотинки. ';
}
else
{
$text .= ' лева и ' . $this->GetText($decimal);
$text .= ' стотинки. ';
}
}
return $text;
}
/**
* Returns text for given number. Parameter should ideally
* be a string (to handle large numbers) though integers are
* OK.
*
* @param string $int Number to convert
* @return string Resulting textual representation
*/
function GetText($int)
{
return $this->ToString($int);
}
/**
* Returns text and number for a randomly generated number.
*
* @return array Array of number and textual representation
*/
function Get()
{
$int = mt_rand(1, 99999);
return array($int, $this->ToString($int));
}
/**
* Returns currency version of a given number.
*
* @param string $int Number to convert
* @param string $major Word to use for left hand side of decimal point
* @param string $minor Word to use for right hand side of decimal point
* @return string Resulting string
*/
function GetCurrency($int, $major = 'pound', $minor = 'pence')
{
if (strpos($int, '.') !== false) {
$left = substr($int, 0, strpos($int, '.'));
$right = substr($int, strpos($int, '.') + 1);
// Plural $major ?
if ((int)abs($left) != 1) {
$major .= 's';
}
$text = $this->GetText($left) . " $major and " . $this->GetText($right) . " $minor";
} else {
$text = $this->GetText($int) . " $major";
}
return $text;
}
}
Как да го ползваме ?
require_once("number2textBG.class.php");
$number = '12345678';
$t2n = new TextualNumber;
$txt = $t2n->GetText($number);
echo $txt;
?>
Източник – http://webstik.com/blog/page/kak-da-preobrazuvame-chislova-stoynost-v-dumi-chrez-php.html