PHP News   Codeschnipsel   PHP-Scripts   Impressum   Datenschutz


Codeschnipsel

Zahlen ins römische wandeln
Dieser Codeschnipsel wandelt arabische Zahlen in römische um.
Hier gibt es eine Vorschau: Demo

Autor: Fabian Karg E-Mailadresse des Autors Gästebuch: kostenlos und werbefrei
<?php
   
function arab2rom($a) {
      if (!
is_numeric($a) OR $a<=OR $a 3999)
         return 
"Diese Zahl kann nicht konvertiert werden.";

      
$q    = array(1000 => 'M',
              
500 => 'D',
              
100 => 'C',
              
50  => 'L',
              
10  => 'X',
              
5   => 'V',
              
1   => 'I',);

      
$x = array ('DCCCC' => 'CM',
                  
'CCCC'  => 'CD',
                  
'LXXXX' => 'XC',
                  
'XXXX'  => 'XL',
                  
'VIIII' => 'IX',
                  
'IIII'  => 'IV',);

      
$r '';

      foreach (
$q as $n => $s) {
         
$b floor($a $n);
         if (
$b 0$r .= str_repeat($s$b);
         
$r strtr($r$x);
         
$a $a $n;
      }
      return 
$r;
   }

   
printf ('<form action="%s" method="POST">'$_SERVER['SCRIPT_NAME']);
   print  (
'<table><tr>');
   
printf ('<td><input type="text" name="arab" value="%s"></td>'$_REQUEST['arab']);
   
printf ('<td>%s</td>', (isset($_REQUEST) AND !empty($_REQUEST)) ? arab2rom($_REQUEST['arab']) : '&nbsp;');
   print  (
'</tr><tr>');
   print  (
'<td>&nbsp;</td><td><input type="submit" name="cmd" value="OK"></td>');
   print  (
'</tr></table>');
?>



Tipps:


Copyright © 2008 phpfriends.net. Alle Rechte vorbehalten.

Notice: Undefined variable: HTTP_SERVER_VARS in /www/htdocs/phpfrien/Counter/counter.php on line 37

Notice: Undefined variable: HTTP_SERVER_VARS in /www/htdocs/phpfrien/Counter/include/config.inc.php on line 64

Notice: Undefined variable: HTTP_SERVER_VARS in /www/htdocs/phpfrien/Counter/include/config.inc.php on line 66

Notice: Undefined variable: HTTP_SERVER_VARS in /www/htdocs/phpfrien/Counter/include/config.inc.php on line 69

Notice: Undefined variable: HTTP_SERVER_VARS in /www/htdocs/phpfrien/Counter/include/config.inc.php on line 71

Deprecated: Assigning the return value of new by reference is deprecated in /www/htdocs/phpfrien/Counter/include/functions_counter.inc.php on line 703