Filter a query string to ensure it is propertly encoded.

Ensures that the values in the query string are properly urlencoded.

@param string $query

@return string
14814
private function filterQuery(string $query): string
    {
        if ($query !== '' && $query[0] === '?') {
            $query = \substr($query, 1);
        }

        $parts = \explode('&', $query);
        foreach ($parts as $index => $part) {
            [$key, $value] = $this->splitQueryValue($part);
            if ($value === null) {
                $parts[$index] = $this->filterQueryOrFragment($key);
                continue;
            }

            $parts[$index] = $this->filterQueryOrFragment($key) . '=' . $this->filterQueryOrFragment($value);
        }

        return implode('&', $parts);
    }
1300307
public function extractPrefixesFromQuery($query)
    {
        preg_match_all('/PREFIX\s+([a-z0-9]+)\:\s*\<([a-z0-9\:\/\.\#\-]+)\>/', $query, $matches);

        $prefixes = [];

        foreach ($matches[1] as $index => $key) {
            $prefixes[$key] = $matches[2][$index];
        }

        return $prefixes;
    }
939329
protected function _filterQuery($query)
    {
        if (! empty($query) && strpos($query, '?') === 0) {
            $query = substr($query, 1);
        }

        $parts = explode('&', $query);
        foreach ($parts as $index => $part) {
            list($key, $value) = $this->_splitQueryValue($part);
            if ($value === null) {
                $parts[$index] = $this->_filterQueryOrFragment($key);
                continue;
            }
            $parts[$index] = sprintf(
                '%s=%s',
                $this->_filterQueryOrFragment($key),
                $this->_filterQueryOrFragment($value)
            );
        }

        return implode('&', $parts);
    }
253542
private static function encode($query, $prefix)
    {
        if (!is_array($query)) {
            return static::rawurlencode($prefix).'='.static::rawurlencode($query);
        }

        $isIndexedArray = static::isIndexedArray($query);
        return implode('&', array_map(function ($value, $key) use ($prefix, $isIndexedArray) {
            $prefix = $isIndexedArray ? $prefix.'[]' : $prefix.'['.$key.']';
            return static::encode($value, $prefix);
        }, $query, array_keys($query)));
    }
515996
public function search($query)
  {
    $content = $query->getBridgeJson($this->clientKey);

    return $this->query($this->bridgeUrl, $content, self::RESULTS_CLASS);
  }
1308633
protected function createUrl($query)
    {
        $options = $this->getOptions();
        $path = sprintf(
            'http://translate.google.com/translate_tts?ie=%s&tl=%s&q=%s',
            $options->hasEncoding() ? $options->getEncoding() : 'UTF-8',
            $options->getLanguage(),
            urlencode($query)
        );
        return $path;
    }
364899
def filter_query_string(query):
    
    return '&'.join([q for q in query.split('&')
        if not (q.startswith('_k=') or q.startswith('_e=') or q.startswith('_s'))])
1166128
protected function setQuery($query)
    {
        $this->uriString = null;

        $query = ltrim($query, '?');

        if (strpos($query, '#') !== false) {
            throw new InvalidArgumentException(
                'Query string must not include a URI fragment.'
            );
        }

        $this->query = static::encode($query);

        return $this;
    }
1133121
protected function query_parse($query)
    {
        $start = $this->options['identifier_start'];
        $end   = $this->options['identifier_end'];
        $quote = self::DEFAULT_QUOTE;

        if ($start == $quote) {
            return $query;
        }

        $pos = 0;
        $in  = false;

        while ($pos = strpos($query, $quote, $pos)) {
            if ($query[$pos+1] == $quote) {  // skip escaped quote
                $pos += 2;
            }
            else {
                if ($in) {
                    $q  = $end;
                    $in = false;
                }
                else {
                    $q  = $start;
                    $in = true;
                }

                $query = substr_replace($query, $q, $pos, 1);
                $pos++;
            }
        }

        return $query;
    }
949275
protected function dropLinkedInParams($query)
    {
        if ($query == '') {
            return '';
        }

        $params = explode('&', $query);
        foreach ($params as $i => $param) {
            /*
             * A key or key/value pair might me 'foo=bar', 'foo=', or 'foo'.
             */
            //get the first value of the array you will get when you explode()
            list($key) = explode('=', $param, 2);
            if (in_array($key, self::$knownLinkedInParams)) {
                unset($params[$i]);
            }
        }

        //assert: params is an array. It might be empty
        if (!empty($params)) {
            return '?'.implode($params, '&');
        }

        return '';
    }
73364
protected function appendUrlQuery($url, $query)
    {
        $glue = $this->urlHasQuery($url) ? '&' : '?';

        return $url.$glue.http_build_query($query);
    }
979661
public function fullUrlWithQuery(array $query)
    {
        parse_str($this->getUri()->getQuery(), $queryParts);

        return (string) $this->getUri()
            ->withQuery(http_build_query($query + $queryParts));
    }
550743
public static String encodeQuery(String query) {
        if (query == null) {
            return null;
        }

        StringBuilder rebuiltQuery = new StringBuilder();

        // Encode parameters to mitigate XSS attacks
        String[] queryParams = query.split("&");
        for (String param : queryParams) {
            String rebuiltParam = encode(param);
            int equalIndex = param.indexOf("=");
            if (equalIndex > -1) {
                String name = param.substring(0, equalIndex);
                String value = (equalIndex < (param.length() - 1)) ? param.substring(equalIndex + 1) : "";
                rebuiltParam = encode(name) + "=" + encode(value);
            }
            if (!rebuiltParam.isEmpty()) {
                rebuiltQuery.append(rebuiltParam + "&");
            }
        }
        // Remove trailing '&' character
        if (rebuiltQuery.length() > 0 && rebuiltQuery.charAt(rebuiltQuery.length() - 1) == '&') {
            rebuiltQuery.deleteCharAt(rebuiltQuery.length() - 1);
        }
        return rebuiltQuery.toString();
    }
1250634
protected function appendQuery($url, $query)
    {
        $query = trim($query, '?&');

        if ($query) {
            $glue = strstr($url, '?') === false ? '?' : '&';
            return $url . $glue . $query;
        }

        return $url;
    }
40402
def urlencode(query, params):
    
    return query + '?' + "&".join(key+'='+quote_plus(str(value)) 
                                  for key, value in params)
706655
protected function applyDefaultFilters($query)
    {
        $sng = SiteTree::singleton();
        foreach ($this->params as $name => $val) {
            if (empty($val)) {
                continue;
            }

            switch ($name) {
                case 'Term':
                    $query = $query->filterAny(array(
                        'URLSegment:PartialMatch' => $val,
                        'Title:PartialMatch' => $val,
                        'MenuTitle:PartialMatch' => $val,
                        'Content:PartialMatch' => $val
                    ));
                    break;

                case 'LastEditedFrom':
                    $fromDate = new DateField(null, null, $val);
                    $query = $query->filter("LastEdited:GreaterThanOrEqual", $fromDate->dataValue().' 00:00:00');
                    break;

                case 'LastEditedTo':
                    $toDate = new DateField(null, null, $val);
                    $query = $query->filter("LastEdited:LessThanOrEqual", $toDate->dataValue().' 23:59:59');
                    break;

                case 'ClassName':
                    if ($val != 'All') {
                        $query = $query->filter('ClassName', $val);
                    }
                    break;

                default:
                    $field = $sng->dbObject($name);
                    if ($field) {
                        $filter = $field->defaultSearchFilter();
                        $filter->setValue($val);
                        $query = $query->alterDataQuery(array($filter, 'apply'));
                    }
            }
        }
        return $query;
    }
405102
private function buildQueryUrlParameter($query)
    {
        switch ($query) {
            case is_array($query) && isset($query['lat']) && isset($query['lon']) && is_numeric($query['lat']) && is_numeric($query['lon']):
                return "lat={$query['lat']}&lon={$query['lon']}";
            case is_array($query) && is_numeric($query[0]):
                return 'id='.implode(',', $query);
            case is_numeric($query):
                return "id=$query";
            case is_string($query) && strpos($query, 'zip:') === 0:
                $subQuery = str_replace('zip:', '', $query);
                return 'zip='.urlencode($subQuery);
            case is_string($query):
                return 'q='.urlencode($query);
            default:
                throw new \InvalidArgumentException('Error: $query has the wrong format. See the documentation of OpenWeatherMap::getWeather() to read about valid formats.');
        }
    }