10.1 Protection of URLs

URLs need to be “percent-encoded” to protect non-ASCII characters, spaces and other ASCII characters. Percent-encoding also allows to have characters be interpreted as part of a path and not as characters with a special role in URLs. For example, ‘?’ has a special role in URLs as it starts a query string. To have it considered as part of a file path, instead of a marker of the beginning of a query, it needs to be percent encoded.

To protect a whole URL, in which characters with a special role in URL are left as is, use url_protect_url_text. To protect file path in URL, including characters with a special role in URLs, use url_protect_file_text.

Function: $protected_url = $converter->url_protect_url_text($input_string)

Percent-encode $input_string, leaving as is all the characters with a special role in URLs, such as ‘:’, ‘/’, ‘?’, ‘&’, ‘#’ or ‘%’ (and a few other). HTML reserved characters and form feeds protected are also protected as entities (see format_protect_text). This is typically used on complete URLs pointing to diverse internet resources, such as the @url URL argument.

for example

return $self->html_attribute_class('a', [$cmdname])
     .' href="'.$self->url_protect_url_text($url)."\">$text</a>";
Function: $protected_path = $converter->url_protect_file_text($input_string)

Percent-encode $input_string leaving as is character appearing in file paths only, such as ‘/’, ‘.’, ‘-’ or ‘_’. All the other characters that can be percent-protected are protected, including characters with a special role in URLs. For example, ‘?’, ‘&’ and ‘%’ are percent-protected. HTML reserved characters and form feeds protected are also protected as entities (see format_protect_text). This is typically used on file names corresponding to actual files, used in the path portion of an URL, such as the image file path in @image.

For example

$self->html_attribute_class('img', [$cmdname])
   . ' src="'.$self->url_protect_file_text($image_file)."\");