Skip to content

Instantly share code, notes, and snippets.

@vadviktor
Created August 30, 2012 14:32
Show Gist options
  • Save vadviktor/3529696 to your computer and use it in GitHub Desktop.
Save vadviktor/3529696 to your computer and use it in GitHub Desktop.
Szám magyar helyesírás szerint
/**
* Számot a magyar helyesírás szabályai szerint szöveggé konvertáló osztály
* maximum százezer-milliárd-os nagyságrenddel számol
*
* @author ikon
*/
class ntw
{
private $singles;
private $tens;
private $tensmods;
private $portions;
public
function __construct()
{
$this->singles = array( "", "egy", "kettő", "három", "négy", "öt", "hat", "hét", "nyolc", "kilenc" );
$this->tens = array( "", "tíz", "húsz", "harminc", "negyven", "ötven", "hatvan", "hetven", "nyolcvan",
"kilencven" );
$this->tensmods = array( "", "tizen", "huszon", "harminc", "negyven", "ötven", "hatvan", "hetven", "nyolcvan",
"kilencven" );
$this->portions = array( "", "ezer", "millió", "milliárd", "ezer" );
}
private
function l2( $str )
{
return mb_convert_encoding( $str, "ISO-8859-2", "UTF-8" );
}
private
function hundredBase( $three )
{
$items = strlen( $three );
//singles /last of parameter is our first number to return
$first_int = $three[ $items - 1 ];
settype( $first_int, "int" );
$hundred = $this->singles[ $first_int ];
//tens
if ( $items > 1 && $first_int > 0 ) //tizen
{
$second = $three[ $items - 2 ];
settype( $second, "int" );
$hundred = $this->tensmods[ $second ] . $hundred;
}
else if ( $items > 1 && $first_int == 0 ) //tíz
{
$second = $three[ $items - 2 ];
settype( $second, "int" );
$hundred = $this->tens[ $second ] . $hundred;
}
//hundreds
if ( $items > 2 )
{
$h = $three[ $items - 3 ];
settype( $h, "int" );
$j = $this->singles[ $h ];
if ( $h > 0 )
{
$j .= "száz";
}
$hundred = $j . $hundred;
}
return $hundred;
}
public
function convertNumberToText( $number )
{
$finished_text = "";
$real_number = $number;
settype( $real_number, "int" );
//splitting the array to the right chunks
$number = strrev( $number );
$number_chunks = str_split( $number, 3 );
for ( $i = 0; $i < count( $number_chunks ); $i++ )
{
$number_chunks[ $i ] = strrev( $number_chunks[ $i ] );
}
$number_chunks = array_reverse( $number_chunks );
//building up final string
for ( $i = 0; $i < count( $number_chunks ); $i++ )
{
$portion_index = count( $number_chunks ) - $i - 1;
$hb = $this->hundredBase( $number_chunks[ $i ] );
//if there are some numbers or if i indicates billion
if ( strlen( $hb ) > 0 || count( $number_chunks ) == 5 && $i == 1 )
{
$finished_text .= $hb . $this->portions[ $portion_index ];
//proper separation
if ( $i < count( $number_chunks ) - 1 )
{
if ( $real_number > 2000 )
{
$finished_text .= "-";
}
}
}
}
//finishedText[0]
return ucfirst( $this->l2( $finished_text ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment