Home > Computing, Programming, php > REGEX For common courier tracking numbers

REGEX For common courier tracking numbers

I was on the hunt not only to find the different variations each courier had for their tracking numbers but also a regex to match. Most of the google fu I found was outdated. Fedex “recently” (months? years? days?) changed from a 12 digit to 15 digit system. And NO, tracking numbers are not totally random. There is usually a space separation on the printed labels you see. Each of the spaced out subsets have a meaning to the courier as well as a checksum. Checksum being, a pre selected sequence of particular number positions added together then divided by a pre selected number. That you can search up yourself if you like. I didn’t find all too much on that matter either.

I am not very good with regex so if there are any suggestions by l33t coderz, they are more than welcome. On with the codes:


/****
[ UPS ]
9 digits, or 1Z+whatever digits
The quick brown fox 1Z9999W99999999999 jumps over the lazy dog.
*/
$ups = '/(\b\d{9}\b)|(\b1Z\d+\b)/';

/****
[ Fedex ]
12 digits, 15 digits, or '96'+ 20 digits
The quick brown fox 999999999999 jumps over the lazy dog.
*/
$fedex = '/(\b96\d{20}\b)|(\b\d{15}\b)|(\b\d{12}\b)/';

/***
[ USPS ]
30 digits, '91'+20 digits, 20 digits (untested)
< TOTALLY UNTESTED BY ME AT THIS TIME >
*/
$usps = '/(\b\d{30}\b)|(\b91\d+\b)|(\b\d{20}\b)/';

I did get the common characters per courier from Packagetrackr. I don’t know how much of it is accurate, but from what I could tell from my experience with FedEx and UPS it seemed to be in line. I was unable to find an all inclusive source.

Categories: Computing, Programming, php Tags:
  1. November 7th, 2012 at 00:27 | #1

    If you don’t have to be responsible for own life, this is your own life a mistake! If you are not responsible
    for their own future, which is your own future a stomp! If you are not responsible for your happiness, which is your
    own happiness of a murder!

  2. March 6th, 2013 at 04:18 | #2

    This is a nice looking feature, but it seems to be broken for me. I attempted the import, I provided my yahoo credentials, I agree to allow access to yahoo and I am shown a list of my delicious bookmarks. When I click the ‘Import Checked Items’ an error is displayed. “an error occurred parseerror: undefined.

  1. No trackbacks yet.