[KLUG Members] PHP and regular expression problem
Adam Williams
members@kalamazoolinux.org
Thu, 30 May 2002 12:52:51 -0400 (EDT)
I've got a nasty programming problem. I'm given a serial number and need
to test if it falls in a certain range of serial numbers. If serial
numbers are entered "correctly" natural ordering works very well, such as
-
$arr1 = array("7LC00001",
"7LC00323",
"7LC00324",
"7LC00378",
"7LC00379",
"7LC99999",
"8AB00001",
"8AB00379",
"8AB00380",
"8AB00608",
"8AB00609",
"8AB00610",
"8AB00611",
"8AB00513",
"8AB99999");
echo "\nNatural order string comparison\n";
usort($arr1,"strnatcmp");
print_r($arr1);
Will properly place 8AB00513 between 8AB00380 and 8AB00608, I just grope
the array and I know the beginning - ending ranges of the "model" I have.
But if I get "8AB513" instead of "8AB00513" it will fall between
"8AB00611" and "8AB99999", which is wrong. So I want to simply gut the
strings of 'embeded' zeros, that is zeros that are then followed by
anything but a zero.
So that:
8AB00610 -> 8AB610
8AB00611 -> 8AB611
8AB00001 -> 8AB1
Then natural ordering will still worth even with absent zeros.
I'm pretty certain (although I'm also mentally fogged at this point) that
$serialnumber = ereg_replace($regexp, "", $serialnumber);
should be able to do it for me, but for the life of me I can't come up
with a $regexp that works.
And expressions to try would be appreciated.