This topic describes the syntax, description, parameters, return values, and examples of string functions.

substr

This function is described as follows:
  • Syntax: substr(s, i, j)
  • Description

    You can call this function to extract parts of a string.

  • Parameters
    • s: the source string to extract from.
    • i: the position to start extraction in the source string, counting from 1. A negative value of -1 specifies the rightmost character of the string. Data type: integer.
    • j: the position to end extraction in the source string, counting from 1. A negative value of -1 specifies the rightmost character of the string. Data type: integer.
  • Return values

    This function returns the substring s[i, j] of s.

  • Examples
    if eq(substr($uri, -5, -1), '.m3u8') {
        say(concat($uri, ' is .m3u8'))
    }
    
    uri_len = len($uri)
    if eq(substr($uri, -5, uri_len), '.m3u8') {
        say(concat($uri, ' is .m3u8'))
    }     
    
    Note: the two methods that are used to determine whether a file is an M3U8 file

concat

This function is described as follows:
  • Syntax: concat(s1, ...)
  • Description

    You can call this function to concatenate strings.

  • Parameters

    s1: the first string to be concatenated. You can specify multiple strings.

  • Return values

    This function returns the concatenated string.

  • Examples
    if eq(substr($uri, -5, -1), '.m3u8') {
        say(concat($uri, ' is .m3u8'))
    }
    
    uri_len = len($uri)
    if eq(substr($uri, -5, uri_len), '.m3u8') {
        say(concat($uri, ' is .m3u8'))
    }     
    
    Note: the two methods that are used to determine whether a file is an M3U8 file

upper

This function is described as follows:
  • Syntax: upper(s)
  • Description

    You can call this function to convert a string to uppercase letters.

  • Parameters

    s: the source string to be converted.

  • Return values

    This function returns the string specified by the s parameter in uppercase letters.

  • Examples
    mystr = 'Hello, Dsl'                                                                                                                                                                                   
    say(upper(mystr))                                                                                                                                                                                      
    say(lower(mystr)) 
    
    
    Output:
    HELLO,DSL
    hello, dsl

lower

This function is described as follows:
  • Syntax: lower(s)
  • Description

    You can call this function to convert a string to lowercase letters.

  • Parameters

    s: the source string to be converted.

  • Return values

    This function returns the string specified by the s parameter in lowercase letters.

  • Examples
    mystr = 'Hello, Dsl'                                                                                                                                                                                   
    say(upper(mystr))                                                                                                                                                                                      
    say(lower(mystr)) 
    
    
    Output:
    HELLO,DSL
    hello, dsl

len

This function is described as follows:
  • Syntax: len(s)
  • Description

    You can call this function to return the length of a string.

  • Parameters

    s: the source string to return the length.

  • Return values

    This function returns the length of the source string specified by the s parameter, which is an integer.

  • Examples
    mystr = 'Hello, Dsl'                                                                                                                                                                                   
    say(upper(mystr))                                                                                                                                                                                      
    say(lower(mystr)) 
    
    
    Output:
    HELLO,DSL
    hello, dsl

byte

This function is described as follows:
  • Syntax: byte(c)
  • Description

    You can call this function to return the ASCII value of a character.

  • Parameters

    c: the character to return the ASCII value for. Only one character can be specified.

  • Return values

    This function returns the corresponding ASCII value, which is of the numeric type.

  • Examples
    say(byte('a'))
    say(byte('A'))
    
    Output: 97
    65

match_re

This function is described as follows:
  • Syntax: match_re(s, p [, o])
  • Description

    You can call this function to use the PCRE engine for regular expression matching.

  • Parameters
    • s: the source string to match. Data type: string.
    • p: the regular expression. Data type: string.
    • (Optional) o: the regular expression engine parameter. Data type: string.
  • Return values

    This function returns true for a successful match and returns false for a failure.

  • Examples
    url = concat('http://', $host, $uri)
    m1 = match_re(url, 'http://. *\.dslex\.com/.*')
    m2 = match_re(url, '^http://. *\.alibaba\.com\.cn/. *\.d\\.html(\?. *)? $')
    m3 = match_re(url, '^http://. *.test.dslex.com/. *\.d\.html(\?. *)? $')
    m4 = match_re(url, '^http://. *\.alibaba\.com\.cn/zt_d/')
    m5 = match_re(url, '^http://tech.alibaba.com.cn/zt_d/we2015/?$')
    m6 = match_re($args, 'from=wap1$')
    m7 = match_re($args, 'from=comos1$')
    
    if and(m1, or(m2, m3), not(m4), not(m5), or(not(m6), not(m7))) {                                                                                                                                       
        add_rsp_header('USER-DEFINED-1', 'hit1')                                                                                                                                                           
        add_rsp_header('USER-DEFINED-2', 'hit2')                                                                                                                                                         

capture_re

This function is described as follows:
  • Syntax: capture_re(s, p [,init])
  • Description

    You can call this function to capture the matches of a string and return the captured substrings. Use the PCRE engine for string matching.

  • Parameters
    • s: the source string to be matched. Data type: string.
    • p: the regular expression for matching. Data type: string.
    • init: the position to start matching, counting from 1. Data type: integer.
  • Return values

    This function returns the matching substrings in the dictionary type for a successful match and returns an empty dictionary for a failure.

  • Examples
    pcs = capture_re($request_uri,'^/([^/]+)/([^/]+)([^?] +)\?(.*)')
    sec1 = get(pcs, 1)
    sec2 = get(pcs, 2)
    sec3 = get(pcs, 3)
    if or(not(sec1), not(sec2), not(sec3)) {
       add_rsp_header('X-TENGINE-ERROR', 'auth failed - missing necessary uri set')
       exit(403)
    }
    digest = md5(concat(sec1, sec3))
    if ne(digest, sec2) {
        add_rsp_header('X-TENGINE-ERROR', 'auth failed - invalid digest')
        exit(403)
    }                                                                                                                                                      

gsub_re

This function is described as follows:
  • Syntax: gsub_re(subject, regex, replace [,option])
  • Description

    You can call this function to replace all matches of a string and return the string after the replacement. Use the PCRE engine for string matching.

  • Parameters
    • subject: the source string to be matched. Data type: string.
    • regex: the regular expression for matching. Data type: string.
    • replace: the string for replacement. Data type: string.
      You can specify the replace parameter by using the matching substrings.
      • $0: specifies all substrings that match regex.
      • $N: specifies the substring that matches the Nth parenthesized subexpression () of regex.
    • (Optional) option: the regular expression engine parameter. Data type: string.
  • Return values

    This function replaces all substrings that match the specified regex parameter in the specified subject parameter with the specified replace parameter and returns the string after the replacement.

  • Examples
    subject = 'Hello, Es'
    regex = '([a-zA-Z])[a-z]+'
    replace = '[$0,$1]'
    add_rsp_header('X-DEBUG-GSUB-RE', gsub_re(subject, regex, replace))
    
    Output:
    X-DEBUG-GSUB-RE: [Hello,H], [Es,E]                                                                                                                                                 
  • See also

    PCRE syntax: https://www.pcre.org/current/doc/html/pcre2syntax.html

split

This function is described as follows:
  • Syntax: split(s [,sep])
  • Description

    You can call this function to split a string into an array of substrings and return the new array.

  • Parameters
    • s: the source string to be split. Data type: string.
    • sep: the separator to use for splitting the string. Data type: string.
  • Return values

    This function returns an array of "key=value" elements in the dictionary type. A number that starts from 1 is used as the value for key. For example, [1]=xx, [2]=yy. If the sep parameter is not specified, the string is separated by any whitespace character.

  • Examples
    if $arg_from {
        t = split($arg_from, ',')
        if get(t, 1) {
            say(concat('[1]=', get(t, 1)))
        }
        if get(t, 2) {
            say(concat('[2]=', get(t, 1)))
        }
    }
    
    Request: ? from=xx1,xx2,xx3  
    Response: [1]=xx1  
    [2]=xx1                                                                                                                                                

split_as_key

This function is described as follows:
  • Syntax: split_as_key(s [,sep])
  • Description

    You can call this function to split a string into an array of substrings and return the new array.

  • Parameters
    • s: the source string to be split. Data type: string.
    • sep: the separator to use for splitting the string. Data type: string.
  • Return values

    Similar with the return values of the split() function. The difference lies in that key is named after each split element: element 1 -> element 2.

  • Examples
    def echo_each(k, v, u) {
        s = concat(k, '=', v, ' u=', get(u, 1))
        say(s) 
    }
    if $arg_from {
        t = split_as_key($arg_from, ',')
        foreach(t, echo_each, ['hi,dsl'])
    }
    
    Request: ? from=xx1,xx2,xx3  
    Response: xx2=xx2 u=hi,dsl
    xx1=xx1 u=hi,dsl
    xx3=xx3 u=hi,dsl                                                                                                                                               

tohex

This function is described as follows:
  • Syntax: tohex(s)
  • Description

    You can call this function to convert a string to a hexadecimal string.

  • Parameters

    s: the string to be converted.

  • Return values

    This function returns the hexadecimal string that is converted from the specified s parameter.

  • Examples
    digest = sha1('xxxx')
    add_rsp_header('X-DSL-TOHEX', tohex(digest))
    
    Note: Add a response header.
    X-DSL-TOHEX: 4ad583af22c2e7d40c1c916b2920299155a46464                                                                                                                                             

tostring

This function is described as follows:
  • Syntax: tostring(a)
  • Description

    You can call this function to convert data of any type to a string.

  • Parameters

    a: the data to be converted. Data type: any type.

  • Return values

    This function returns the string that is converted from the specified a parameter.

  • Examples
    s = tostring(123)
    add_rsp_header('X-DSL-TOSTRING', s)
    
    Note: Add a response header.
    X-DSL-TOSTRING: 123                                                                                                                                            

tochar

This function is described as follows:
  • Syntax: tochar(n1, n2, ...)
  • Description
    • You can call this function to convert one or more internal integers (ASCII values) to a string. For example, 48 corresponds to the character "0".
    • The length of the returned string is determined by the number of the specified parameters.
  • Parameters

    nX: the integer to be converted. You can specify multiple integers.

  • Return values

    This function returns the converted string.

  • Examples
    add_rsp_header('X-DSL-TOCHAR', tochar(97))
    add_rsp_header('X-DSL-TOCHAR', tochar(97, 98), true)
    
    Output: Add the following response headers.
    X-DSL-TOCHAR: a
    X-DSL-TOCHAR: ab
    
    if $arg_filename {
        hn = 'Content-Disposition'
        add_rsp_header('Content-Disposition', concat('attachment;filename=', tochar(34), filename, tochar(34)))
        add_rsp_header(hn, hv)
    }
    
    Output: Add the response header.
    Content-Disposition: attachment;filename="The value of the filename parameter"