Cut Off Last Character Of EXTEN

Home » Asterisk Users » Cut Off Last Character Of EXTEN
Asterisk Users 10 Comments

Hello,

how can I cut off the last character of the EXTEN-variable with variating length ?

So I have :

112233#
123#
123456789#

I want to cut off the last character.

${EXTEN:-1} gives me #, but that is the character I want to cut off.

Kind regards, Jonas.

10 thoughts on - Cut Off Last Character Of EXTEN

  • How about “${EXTEN:-1:1}”?

    “The Definitive Guide” has a special paragraph with the title “*More Advanced Digit Manipulation”.*

    jg

  • Hi, try

    [testEx]
    exten => _X.,1,noop(${EXTEN})
    same => n,noop(len: ${LEN(${EXTEN})})
    same => n,noop(${EXTEN:0:${MATH(${LEN(${EXTEN})}-1)}})
    exten => _X.,n,hangup

  • From: asterisk-users-bounces@lists.digium.com
    [mailto:asterisk-users-bounces@lists.digium.com] On Behalf Of Jonas Kellens Sent: Tuesday, August 20, 2013 4:29 AM
    To: Asterisk Users Mailing List – Non-Commercial Discussion Subject: [asterisk-users] Cut off last character of EXTEN

    Hello,

    how can I cut off the last character of the EXTEN-variable with variating length ?

    So I have :

    112233#
    123#
    123456789#

    I want to cut off the last character.

    ${EXTEN:-1} gives me #, but that is the character I want to cut off.

    Kind regards, Jonas.

    Here ya go:

    112233# use ${EXTEN:0:6})

    123# use ${EXTEN:0:3})

    123456789# use ${EXTEN:0:9})

  • On 20 Aug 2013, at 12:25, Pat Collins wrote:

    I think ‘variable length’ implied ‘unknown length’…

    S

  • From: asterisk-users-bounces@lists.digium.com
    [mailto:asterisk-users-bounces@lists.digium.com] On Behalf Of Steven Howes Sent: Tuesday, August 20, 2013 7:38 AM
    To: Asterisk Users Mailing List – Non-Commercial Discussion Subject: Re: [asterisk-users] Cut off last character of EXTEN

    On 20 Aug 2013, at 12:25, Pat Collins wrote:

    Here ya go:

    112233# use ${EXTEN:0:6})

    123# use ${EXTEN:0:3})

    123456789# use ${EXTEN:0:9})

    I think ‘variable length’ implied ‘unknown length’…

    S

    Yea, I realized that after I replied….

    Got ahead of myself again.

    That was the only way I was able to get rid of the ‘#’ tho….

  • Yes; but the whole point is, the original poster does not know the length of the number in the first place!

    What is needed is
    ${EXTEN:0:-1}
    which will skip no digits from the beginning, and use all but one digit of the remainder.

    Now, here’s my handy cut-out-and-keep guide to the syntax for the implied substring method in Asterisk dialplan:

    Write
    ${VARIABLE:skip}
    to skip some characters and use everything else, or
    ${VARIABLE:skip:show}
    to skip some characters and only use part of the remainder. And a minus sign means “all but”. Some recipes, by way of example;

    When dealing with international calls,
    ${EXTEN:2}
    will remove the “00” from the beginning (skip 2 digits).

    When routing direct dial-in numbers,
    ${EXTEN:-3}
    gets the last 3 digits of the dialled number (skip all but 3 digits).

    If the number allocation has been crafted carefully so that (for argument’s sake) numbers in the range 100 – 199 are management and accounts, 200 – 299
    are sales, 300 – 399 are purchasing, 400 – 499 are IT and buildings maintenance and 500 – 599 are factory floor, then
    ${EXTEN:-3:1}
    will indicate the department (skip all but 3, and use only the first digit).
    You could also write ${EXTEN:-3:-2} (skip all but 3, and use all but 2 of the remainder) but since you already know the length anyway, this is probably a little pointless.

    To remove both a dialled “9” from the beginning and a terminating comment mark from the end (skip one and use all but one) then the required syntax would be
    ${EXTEN:1:-1}