array_split

The `array_split` directive splits a string variable into an array based on a specified separator.

Syntaxarray_split to=$target_variable;
Defaultnone
Contexthttp, server, location, if in server, if in location
Arguments2+

Description

The array_split directive is utilized in NGINX configurations to divide a string into an array, with the array elements determined by a specified separator. This directive is particularly useful for processing values from request parameters, such as query strings, where multiple values are sent as a single string separated by a delimiter. The syntax requires at least two arguments: the separator string used to delimit the input string, and the subject string (typically a variable that holds the input values). Additionally, the results are stored in a new variable specified by the to parameter, which will hold the resulting array and can be leveraged later in the NGINX processing pipeline within any directives that support array variables.

Under the hood, the array_split directive operates by embedding NGINX string values to manage pointers to C data types, specifically the ngx_array_t structure, enabling NGINX to handle lists of values more effectively. The resulting array-typed variable can only be used with other directives provided by the Array-typed variables module, and thus for operations outside this context, one must use the array_join directive to convert the array back into a standard string format for usage in locations outside the realm of array operations. Overall, this functionality significantly enhances the scripting capabilities of NGINX configurations by allowing seamless manipulation of list-like structures within server configurations.

Config Example

location /foo {
    array_split ',' $arg_files to=$array;
    array_map "name = $array_it" $array;
    array_join ' or ' $array to=$sql_condition;
    echo "select * from files where $sql_condition";
}

The to parameter must specify a variable that will hold the resulting array; ensure that it follows the assignment syntax correctly.

The resulting array-typed variable can only be utilized with directives from this module; attempting to use it in other directives will result in an error.

Ensure the separator accurately reflects the intended delimiter in the input string, as any incorrect separator will yield an unexpected array structure.

← Back to all directives