[Asterisk-Dev] Algorithm to parse country code

Robert Bedell robert.bedell at zivVa.com
Mon May 10 09:15:02 MST 2004


Hey all,

TP> I am trying to parse a dialed extension in order to deduce the country
TP> code - but can't seem to find any references on efficient ways to do
TP> this... though surely there is a well known good algorithm for this
TP> already?

TP> I plan to store my country details in a MySQL database. Conceptually,
TP> i've already thought that I could successfully match a country from the
TP> dialled extension by reading the countries into an array (or b-tree or
TP> something), and then going through the list several times starting with
TP> the four digit codes, then three digit ones, then two, then one etc...
TP> until a match is made. I assume that country codes do not overlap in a
TP> way which would make this not possible.

TP> Is there a better way to do this? Even if I read all entries and store
TP> them in a tree, I still have to potentially go through the tree four
TP> times to reach the single digit country codes (most notably USA/Canada)
TP> - and every call of the PHP script would search the SQL
TP> database/re-populate the tree... surely this would be very expensive
TP> computationally?

TP> I don't recall a feature in MySQL that allows you to query x number of
TP> characters of a given field only.

TP> Anyone have any better ideas?

TP> Thanks

TP> - Terence

MT> DOnt' clearly understood what you want, but maybe you need this?

MT> If you have table like:

Call this "routes"
MT> route   country
MT> 1       USA
MT> 1212    USA-NY
MT> 49      Germany

MT> and you have called number like:
MT> 12121234567

We do something similar here.  Why not use substring on the entire dialed
extension?  If the 'route' is unique in the table above, the first row of
the following query will give you the longest possible match on a given
extension:

  select *
    from routes
   where route=substring('12121234567',0,char_length(route))
   order by length(route) desc

Cheers!

Robert




More information about the asterisk-dev mailing list