You are here

function encode_url in TMGMT Translator Smartling 8

This function encodes URL to form a URL which is properly percent encoded to replace disallowed characters.

RFC3986 specifies the allowed characters in the URL as well as reserved characters in the URL. This function replaces all the disallowed characters in the URL with their repective percent encodings. Already encoded characters are not encoded again, such as '%20' is not encoded to '%2520'.

Parameters: url the url to encode.

Return values: Returns the encoded URL string.

File

includes/url_to_absolute.inc, line 539
Edited by Nitin Kr. Gupta, publicmind.in

Code

function encode_url($url) {
  $reserved = array(
    ":" => '!%3A!ui',
    "/" => '!%2F!ui',
    "?" => '!%3F!ui',
    "#" => '!%23!ui',
    "[" => '!%5B!ui',
    "]" => '!%5D!ui',
    "@" => '!%40!ui',
    "!" => '!%21!ui',
    "\$" => '!%24!ui',
    "&" => '!%26!ui',
    "'" => '!%27!ui',
    "(" => '!%28!ui',
    ")" => '!%29!ui',
    "*" => '!%2A!ui',
    "+" => '!%2B!ui',
    "," => '!%2C!ui',
    ";" => '!%3B!ui',
    "=" => '!%3D!ui',
    "%" => '!%25!ui',
  );
  $url = rawurlencode($url);
  $url = preg_replace(array_values($reserved), array_keys($reserved), $url);
  return $url;
}