[Asterisk-Dev] Algorithm to parse country code

Stephen Davies steve at daviesfam.org
Mon May 10 22:00:58 MST 2004


On Mon, 10 May 2004, Terence Parker wrote:

> I am trying to parse a dialed extension in order to deduce the country
> code - but can't seem to find any references on efficient ways to do
> this... though surely there is a well known good algorithm for this
> already?
> 
> I plan to store my country details in a MySQL database. Conceptually,
> i've already thought that I could successfully match a country from the
> dialled extension by reading the countries into an array (or b-tree or
> something), and then going through the list several times starting with
> the four digit codes, then three digit ones, then two, then one etc...
> until a match is made. I assume that country codes do not overlap in a
> way which would make this not possible.
> 
> Is there a better way to do this? Even if I read all entries and store
> them in a tree, I still have to potentially go through the tree four
> times to reach the single digit country codes (most notably USA/Canada)
> - and every call of the PHP script would search the SQL
> database/re-populate the tree... surely this would be very expensive
> computationally?
> 
> I don't recall a feature in MySQL that allows you to query x number of
> characters of a given field only.
> 
> Anyone have any better ideas?

Look at the way app_prepaid does it.  They store the country codes in
a DB, then the where clause goes:

  where substring(dialednumber, 1, length(countryprefix)) =
    countryprefix

to find the match.

Steve





More information about the asterisk-dev mailing list