Page 168 - Asterisk™: The Future of Telephony
P. 168
If that one left you scratching your head, look at it again. Did you notice the period on
the end? This pattern matches any number that starts with 011 and has at least one
more digit. In the NANP, this indicates an international phone number. (We’ll be using
these patterns in the next section to add outbound dialing capabilities to our dialplan.)
Using the ${EXTEN} channel variable
We know what you’re thinking… You’re sitting there asking yourself, “So what happens
if I want to use pattern matching, but I need to know which digits were actually dialed?”
Luckily, Asterisk has just the answer. Whenever you dial an extension, Asterisk sets
the ${EXTEN} channel variable to the digits that were dialed. We can use an application
called SayDigits() to test it out:
exten => _XXX,1,SayDigits(${EXTEN})
In this example, the SayDigits() application will read back to you the three-digit ex-
tension you dialed.
Often, it’s useful to manipulate the ${EXTEN} by stripping a certain number of digits off
the front of the extension. This is accomplished by using the syntax ${EXTEN:x}, where
x is where you want the returned string to start, from left to right. For example, if the
value of EXTEN is 95551212, ${EXTEN:1} equals 5551212. Let’s take a look at another
example:
exten => _XXX,1,SayDigits(${EXTEN:1})
In this example, the SayDigits() application would start at the second digit, and thus
read back only the last two digits of the dialed extension.
More Advanced Digit Manipulation
The ${EXTEN} variable properly has the syntax ${EXTEN:x:y}, where x is the starting
position, and y is the number of digits to return. Given the following dial string:
94169671111
we can extract the following digit strings using the ${EXTEN:x:y} construct:
${EXTEN:1:3} would contain 416.
${EXTEN:4:7} would contain 9671111.
${EXTEN:-4:4} would start four digits from the end, and return four digits, giving us
1111.
‡ If you grew up in North America, you may believe that the 1 you dial before a long distance call is “the long
distance code.” This is incorrect. The number 1 is in fact the international country code for all countries in
NANP. Keep this in mind if you ever send your phone number to someone in another country. They may
not know what your country code is, and thus be unable to call you with just your area code and phone
number. Your full phone number with country code should be printed as +1 NPA NXX XXXX (where NPA
is your area code)―e.g., +1 416 555 1212.
140 | Chapter 5: Dialplan Basics