Url

url_encode

Percent-encode a string for use in a URL query string. Encodes all characters except unreserved: A-Z a-z 0-9 - _ . ~

Arguments

  • $1 (string): Value to encode

Exit codes

  • 0: Always

Output on stdout

  • Percent-encoded string

url_decode

Decode a percent-encoded URL string. Also converts '+' to space (application/x-www-form-urlencoded convention).

Arguments

  • $1 (string): Percent-encoded string

Exit codes

  • 0: Always

Output on stdout

  • Decoded string

url_parse_query

Parse a URL query string into key=value lines. With -n , populates a named associative array instead. Keys and values are percent-decoded. For repeated keys, the last value wins when writing to an associative array.

Example

url_parse_query 'name=Alice&city=Auckland'
# => name=Alice
# => city=Auckland

Arguments

  • $1 (string): Optional: '-n ' to specify target associative array name
  • $2 (string): Query string (e.g. 'foo=bar&baz=qux'), or $1 if -n not used

Exit codes

  • 0: Success
  • 2: Missing argument

Output on stdout

  • key=value lines (without -n)

url_get_param

Extract a single parameter value from a URL query string.

Arguments

  • $1 (string): Query string (e.g. 'foo=bar&baz=qux')
  • $2 (string): Parameter key to look up

Exit codes

  • 0: Key found
  • 1: Key not found

Output on stdout

  • Parameter value (percent-decoded), or empty if not found

url_build_query

Build a percent-encoded query string from key=value arguments. Keys and values are each percent-encoded. Output does not include a leading '?'.

Example

url_build_query name=Alice city=Auckland
# => name=Alice&city=Auckland

Arguments

  • ... (string): One or more 'key=value' pairs

Exit codes

  • 0: Always
  • 1: No arguments

Output on stdout

  • Percent-encoded query string