<?php
// echo  samTxt( "999999999999");

 function  samTxt($TheNo){
   $MyCur = "ريال "; $MySubCur = "هللة " ;
      // $MyArry1 = array("","مائة","مائتان","ثلاثمائة","أربعمائة","خمسمائة","ستمائة","سبعمائة","ثمانمائة","تسعمائة");
      $MyArry1 = array();
      $MyArry1[0] = "";
      $MyArry1[1] = "مائة";   $MyArry1[2] = "مائتان";   $MyArry1[3] = "ثلاثمائة"; $MyArry1[4] = "أربعمائة"; $MyArry1[5] = "خمسمائة";
      $MyArry1[6] = "ستمائة"; $MyArry1[7] = "سبعمائة";  $MyArry1[8] = "ثمانمائة"; $MyArry1[9] = "تسعمائة";
      // $MyArry2 = array("","عشرة","عشرون","ثلاثون","أربعون","خمسون","ستون","سبعون","ثمانون","تسعون");
      $MyArry2 = array();
      $MyArry2[0] = "";      $MyArry2[1] = "عشرة"; $MyArry2[2] = "عشرون"; $MyArry2[3] = "ثلاثون"; $MyArry2[4] = "أربعون";
      $MyArry2[5] = "خمسون"; $MyArry2[6] = "ستون"; $MyArry2[7] = "سبعون"; $MyArry2[8] = "ثمانون"; $MyArry2[9] = "تسعون";
      // $MyArry3 = array("","واحد","اثنان","ثلاثة","أربعة","خمسة","ستة","سبعة","ثمانية","تسعة");
      $MyArry3 = array();
      $MyArry3[0] = "";      $MyArry3[1] = "واحد"; $MyArry3[2] = "اثنان"; $MyArry3[3] = "ثلاثة"; $MyArry3[4] = "أربعة";
      $MyArry3[5] = "خمسة"; $MyArry3[6] = "ستة"; $MyArry3[7] = "سبعة"; $MyArry3[8] = "ثمانية"; $MyArry3[9] = "تسعة";
$Myno ="";
$GetNo = "";
$RdNo ="";
$My100 ="";
$My10 ="";
$My1 ="";
$My11 ="";
$My12 ="";
$GetTxt ="";
$Mybillion ="";
$MyMillion ="";
$MyThou ="";
$MyHun ="";
$MyFraction ="";
$MyAnd =" و ";

$ReMark ="";
$result="";

if ( $TheNo > 999999999999.99 ){ exit();}
if ($TheNo < 0) { $TheNo = $TheNo * -1 ; $ReMark = "سالب " ;}
  else {$ReMark = "فقط  " ;}
if ($TheNo == 0) { $NoToTxt = " صفر " ; exit();}
$TheNo = number_format((float)$TheNo, 2, '.', '');
$GetNo = str_pad($TheNo, 15, '0', STR_PAD_LEFT);
$i = 0;
While ($i < 15){
    if ($i < 12 ){  $Myno = substr($GetNo, $i , 3); }
      else {$Myno = 0 . substr($GetNo, $i+1, 2) ; }
      $RdNo = 0 ;
if ((substr($Myno, 0, 3)) > 0) {
  $RdNo   = substr($Myno, 0, 1);
  $My100  = $MyArry1[$RdNo];
  $RdNo   = substr($Myno, 1, 1);
  $My10    = $MyArry2[$RdNo];
  $RdNo   = substr($Myno, 2, 1);
  $My1   = $MyArry3[$RdNo];

    if (substr($Myno, 1, 2) == 11){ $My11 = "إحدي عشر" ;}
    if (substr($Myno, 1, 2) == 12 ){ $My12 = "إثني عشر" ;}
    if (substr($Myno, 1, 2) == 10) { $My10 = "عشرة" ;}

    if (((substr($Myno, 0, 1)) > 0) && ((substr($Myno, 1, 2)) > 0)) {$My100 = $My100 .$MyAnd ;}
    if (((substr($Myno, 2, 1)) > 0) && ((substr($Myno, 1, 1)) > 1)) {$My1 = $My1 . $MyAnd ;}

    $GetTxt = $My100 .$My1 . $My10 ;

          if (((substr($Myno, 2, 1)) == 1) && ((substr($Myno, 1, 1))) == 1){
            $GetTxt = $My100 . $My11 ;
            if ((substr($Myno, 0, 1)) == 0) { $GetTxt = $My11 ;}
          }
          if (((substr($Myno, 2, 1)) == 2) && ((substr($Myno, 1, 1))) == 1) {
            $GetTxt = $My100.$My12 ;
            if ((substr($Myno, 0, 1)) == 0){ $GetTxt = $My12 ;}
          }

      if (($i == 0)&& ($GetTxt != "")){
                  if ((substr($Myno, 0, 3)) < 10) {  $Mybillion = $GetTxt . " مليار " ;}
                    else{
                  $Mybillion = $GetTxt . " مليارات " ;
                    if ((substr($Myno, 0, 3)) == 2) {$Mybillion = " مليار " ;}
                      if ((substr($Myno, 0, 3)) == 2) {$Mybillion = " ملياران" ;}
                        }
                  }

                    if (($i == 3)&& ($GetTxt != "")){

                          if ((substr($Myno, 1, 3)) > 10) { $MyMillion = $GetTxt . " مليون " ;}
                            else{
                                $MyMillion = $GetTxt . " مليون " ;
                                  if ((substr($Myno, 0, 3)) == 1) {$MyMillion = " مليون " ;}
                                  if ((substr($Myno, 0, 3)) == 2) {$MyMillion = " مليونان " ;}
                                }
                    }

          if (($i == 6) && ($GetTxt != "")){
              if (((substr($Myno, 0, 3)) > 10)){ $MyThou = $GetTxt . " ألف " ;}
                else{
                    $MyThou = $GetTxt . " ألاف " ;
                if ((substr($Myno, 2, 1)) == 1){$MyThou = " ألف " ;}
                if ((substr($Myno, 2, 1)) == 2) {$MyThou = " ألفان " ;}
                          }
                    }

                  if (($i == 9) && ($GetTxt != "")) {$MyHun = $GetTxt ;}
                  if (($i == 13)&& ($GetTxt != "")) {$MyFraction = $GetTxt ;}
}
  $i = $i + 3 ;
}

if ($Mybillion != "") {
if (($MyMillion != "")||($MyThou != "") Or ($MyHun != "")) {$Mybillion = $Mybillion . $MyAnd ;}
}

if ($MyMillion != "") {
    if (($MyThou != "")||($MyHun != "")) {$MyMillion = $MyMillion . $MyAnd ;}
}

if ($MyThou != "") {
    if ($MyHun != "") {$MyThou = $MyThou . $MyAnd ;}
}

if ($MyFraction != "") {
    if (($Mybillion != "")||($MyMillion != "")||($MyThou != "")||($MyHun != "")){
        $result = $ReMark . $Mybillion .$MyMillion . $MyThou . $MyHun . " " . $MyCur . $MyAnd . $MyFraction . " " . $MySubCur ; }
        else{
        $result = $ReMark . $MyFraction . " " . $MySubCur ;
      }
} else {
$result = $ReMark . $Mybillion . $MyMillion . $MyThou . $MyHun . " " . $MyCur ;
}
return $result ;
}
 ?>
