I want to change a couple off characters * # for A and P to have the monitor filename with characters more friendly. The only solution I could find was to do it my self within the dialplan but it generates a lot of verbosity output and is not as efficient(fast) as I would like to. I'll post it here just in case someone wants to use it. But I'm looking for an asterisk function that I can compile something that I can call withing the dialplan like ${REPLACE(${EXTEN},*,a)} and have the exten **123**456*** converted to AA123AA456AAA.
;
; MACRO REPLACE
;
[macro-replace]
;
; ${ARG1} - String source
; ${ARG2} - Chars to replace
; ${ARG3} - Chars to replace with
;
exten => s,1,NoOp(Replacing ${ARG2} for ${ARG3} in ${ARG1})
exten => s,n,Set(str=${ARG1})
exten => s,n,Set(find=${ARG2})
exten => s,n,Set(replace=${ARG3})
exten => s,n,Set(i=0)
exten => s,n,Set(length=${LEN(${str})})
exten => s,n,While($[${i} < ${length}])
exten => s,n,GotoIf($["${str:${i}:1}" != "${find}"]?continue)
exten => s,n,Set(pre=)
exten => s,n,GotoIf($["${i}" = "0"]?post)
exten => s,n,Set(pre=${str:0:${i}})
exten => s,n(post),Set(post=)
exten => s,n,GotoIf($["${i}" = $[${length} - 1]]?write)
exten => s,n,Set(post=${str:$[${i} + 1]})
exten => s,n(write),Set(str=${pre}${replace}${post})
exten => s,n(continue),Set(i=$[${i} + 1])
exten => s,n,EndWhile
exten => s,n,Set(REPLACERESULT=${str})
The REPLACE() function now does this easily:
Output:
That's really the best way to do it (without using regex). If you want to use regex (regular expressions), Asterisk 1.1+ has full support for it. This will allow you to do your entire macro in a single line. The documentation for using regex in dialplan is here: voip-info.
Hopefully this helps! There are plenty of examples on that voip-info page that should be able to help you along!
Another alternative to what you've done is to use an AGI script. Just write your code in bash/python/etc and use it as AGI(replace,${arg1},${arg2},${arg3}). Might not be as fast as an internal function but it's more compact and potentially faster than your solution.