PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » Validate » Validate » Bug #4991

Roman numeral validation

Details

Request #4991Roman numeral validation
Submitted2005-08-04 00:16 UTC
Fromjoe at joestump dot net
Assignedpajoye
StatusWont fix
PackageValidate
PHP VersionIrrelevant
OSAll
Roadmaps(Not assigned)

Comments

[2005-08-04 00:16 UTC] joe at joestump dot net

Description:
------------
The function Numbers_Roman::toNumber() does not validate
whether or not the given string is even a valid roman numeral.
For instance, XXXXX isn't valid. It should be L.

I'd either create Numbers_Roman::isValid() or add the
validation code to toNumber() and have it return false, 0 or
PEAR_Error. Here is the proper regexp (appears to work great):

^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$

Maybe this should be part of the Validate package?

Test script:
---------------
<?php

require_once 'Numbers/Roman.php';

echo Numbers_Roman::toNumber('XXXXX')."\n";
echo Numbers_Roman::toNumber('L')."\n";
echo Numbers_Roman::toNumeral(30)."\n";

?>

Expected result:
----------------
First echo should be false, 0 or a PEAR_Error

Actual result:
--------------
It returns 50, which *technically* is true (5 * X = 50), but
the number itself is invalid.